C++ Tutorial #5 – Switch Statements

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Today will be an extremely quick & easy tutorial; we will be talking about switch statements. Switch statements are very similar to if statements, but they are written differently. Here’s an example on what a switch statement looks like:

 

 

If you remember the last tutorial on if statements; after writing “if”, you had to compare two variables inside of the parenthesis (otherwise known as the condition). In switch statements, you can’t compare two variables. Instead, switch statements just checks if the variable you placed inside of the parenthesis (otherwise known as the expression), is equal to the case. If the variable is equal; the program will run the statement inside of that case. If the variable is not equal; it checks the next case, and the next one, until you reach to default. When that happens, and the program runs out of cases to check, it stops and runs the statement inside default (similar to the “else” in if statements). Also notice after every case, you write “break” after the statement, however you do not have to use “break” in default.

 

That’s pretty much how switch statements work. Lets write a program using switch statements. Again, you can copy and paste the code, but I recommend you write it out yourself so you can learn better.

 

 

Here’s our result:

 

Switch Statment Result Image1Switch Statment Result Image2

 

I used a “char” instead of an “int” just to show that you can can use different types of variables. Make sure you place single quotes around the characters in your cases.

 

Let me add this fact:

 

For some reason in C++, you can’t use strings in switch statements, but in the C# language you can. You can use strings in an if statement instead.

 

I’ll explain a little on what the code does, but most of it should be pretty easy.

 

 

We created a char variable called “letter” and did not assign it a value yet. We used “cout” to tell the user to enter a letter; “cin” is used so the user can assign a value, or character to the variable “letter”.

 

 

Then we get to our switch statement. The program checks if our character we chose is equal to a, b, c, d, e, f, & g. If it is equal to any of these letters, the program will run the statement in that case. If it’s not equal to any of these letters, it will run the statement after default.

 

Thank you viewers, for reading my posts. I will end this tutorial here today. Be sure to check my site for new posts in the future. I try to write every Tuesday and Saturday nights. If you want to you can subscribe to the mailing list (on the home page) and get notified on my new posts via email. See you soon.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.