article

Basics of C/C++ Part 5: Switch...Case

Email
Submitted on: 1/1/2015 7:37:00 AM
By: Alexander of CProgramming.com (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general)
Views: 1021
 
     I know that this is probably a let-down, after you learned all about functions, but switch...case is important to know. After all, it can save space with if statements, and it is useful. Besides, I couldn't think of anything else that I wanted to write about!

 
				

Switch...case looks like this:

switch(expression or variable)

{

case it equals this:

do this;

break;

case it equals this:

do this;

break;

case it equals this:

do this;

break;

...

default

do this

}

So, it works like this. The expression or variable has a value. The case says that if it has the value of whatever is after this case then do whatever follows the colon. The break says to break out of the case statements. Break is a keyword that breaks out of the code-block, surrounded by braces, that it is in. So unless you want it to try the next case then use break. --You can also use it to break out of loops, something that I failed to mention at the time.--

What is it used for, the switch...case? Well, let's say that you are writing a menu program, then you would want to process some input, right? Well, you would want to use a switch...case statement to process more than one input, because it is more easily used than if statements.

Here is an example program:

#include

#include

void main()

{

char input;

cout<<"1. Play game";

cout<<"2. Load game";

cout<<"3. Play multiplayer";

cout<<"4. Exit";

input=getch(); //Remember I said you don't need many functions...

switch(input)

{

case 1:

playgame();

break;

case 2:

loadgame();

break;

case 3: //Note use of : not ;

playmultiplayer();

break;

case 4:

break;

default:

cout<<"Error, bad input, quitting";

}

}

If you are not understand this, then try putting in if statements for the case statements. Also, the reason exit works with a break is that after you break out of the switch statement then it would be the end of the program. The same thing would be for default. If you don't like that, then make a loop around the whole thing. I know I did not prototype the functions, but it was a very simple example. You could easily make a few small functions.


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.