All right. In the previous tutorial I covered control flow and using if statements. This time I'm going to cover switch.
What is a switch? Well it's another form of an if statement, I gues it's a cleaner form of an if statement.
Example.cpp
- Code:
-
#include <iostream>
using std::cout;
using std::cin;
int main()
{
cout << "Enter integer (1 - Exit, 2 - Travel through time, 3 - Get a girl, 4 . drive drunk)->";
int x;
cin >> x;
if (x == 1)
{
close();
}
else if (x == 2)
{
TimeMachine();
}
else if (x == 3)
{
BadPickupLinesOnReallyHotGirls();
}
else if (x == 4)
{
Die();
}
else
cout << "Input not recognized\n";
return 0;
}
Okay a bunch of beautiful if and if else statements right?
Actually wrong. It just looks very ugly, repetitive and unprofessional.
Now we can pull off the same exact set of instructions using switch statements.
- Code:
-
int x;
switch(x) //Consider switch as the if and x is what the values will be compared to.
{ //Begin the switch block
case 1: //condition user enters 1 began by a colin
{ //Begin the block
close();
break; //I will explain break; after. but pay attention to it.
}//End the case block
case 2: //Condition user enters 2
{
TimeMachine();
break;
}
case 3:
{
BadPickupLinesOnReallyHotGirls();
break;
}
case 4:
{
Die();
break;
}
default: //I will explain this in a second
cout << "Input not recognized\n";
} //end the switch block
That is what a switch statement looks like, and depending on what values the user enters is where the case comes in.
If I wanted to jump off a bridge when the user enters 100 for x, I can do.
- Code:
-
switch(x)
{
case 100:
{
JumpOffABridge();
break;
}
default:
cout << "I did not understand what you said";
}
Okay if you haven't guesd by now. At the end of both switch statements the last instruction I put was a default. What is a default? It's exactly like an else. In my if else statements above I had an if, if else, if else, if else, and an else for the final condition meaning the program didn't meat any of the conditions above. That's what a default does.
In the example above, it shows case 100:
only if the user enters 100 will I jump off a bridge. But let's say we entered a value that we didn't have a condition for in our switch statement? Then the program jumps to the default and can run a last set of instructions before ending the switch.
Now what is a break? Well a break ends the switch instructions. So why do we need it?
Let's say did this.
- Code:
-
int x;
switch(x) //Consider switch as the if and x is what the values will be compared to.
{ //Begin the switch block
case 1: //condition user enters 1 began by a colin
{ //Begin the block
close();
}//End the case block
case 2: //Condition user enters 2
{
TimeMachine();
}
case 3:
{
BadPickupLinesOnReallyHotGirls();
}
case 4:
{
Die();
}
default: //I will explain this in a second
cout << "Input not recognized\n";
} //end the switch block
There are no breaks in that switch. So the program runs, asks me to enter a value. I enter 2. Oh woo! A condition was met If we enter 2 we call the function TimeMachine(); So the function TimeMachine() runs, and after how ever many instructions, it ends. Now when it ends we covered in our previous tutorial that the program will go back to the main function and continue where it left off.
Can you gues where we will continue from after the function TimeMachine(); is ran?
If you gused inside the switch statement, you are absolutely correct. Because even though the condition was met, we never told the switch to end. What will it do? Continue checking if condition 3 was met, and then continue checking if condition 4 was met, and then defualt. But that's unnecessary since we all ready entered 1. So what do we do to prevent that?
We enter a break at the end of each case, so when the condition is met, it will run the instructions you told it, let's say function TimeMachine(); come back to the main function and realize there is a break. So no more needing to check through the switch statement we can just break out of it and move on.
That is pretty much all I can cover about switch statements. I hope I did not cause any confusion. If you have any questions feel free to ask.