C++ Tutorial for noobs - Part 6: If, Else and Switch
Motivation
Controlling the Flow of our programs is another fundamental aspect amongst all kinds of programming languages.
Our goal is to find a way to make decisions and execute different code depending on the result. A quick example demonstrates how a program's welcome message could work:
if today is birthday then
show a 'happy birthday' message
else
show the usual 'welcome' message
If and Else in C++
The above example is almost C++ code already. Here is how if-and-else control statements work:
int day = 42;
if (day == 120)
{
std::cout<<"happy birthday!";
}
else
{
std::cout<<"welcome!";
}
So as we can see, it always starts with the word if, followed by some kind of condition inside of ( and ), followed by some code surrounded with { and }, which will be executed if the condition holds true. Furthermore C++ allows us to add an else case, which is followed by { and } again, this time for the code that will be execute if the condition is not true.
The Condition
The condition is a test that gives us either true or false. In our case, the condition
day == 120
is true if 'the day equals 120' or false otherwise. The reason why we use == instead of = for 'equals' is that we already use = to assign things to variables like this:
int day = 42;
We can also combine conditions like this:
if (day == 120 || day == 130)
{
std::cout<<"happy birthday!";
}
which is true 'if day is 120 or day is 130'. Hence the user would see a birthday message once on day 120 and once on day 130.
We can also combine conditions with and like this:
if (day == 120 && hour == 0)
{
std::cout<<"happy birthday!";
}
which is only true if 'the day is 120 and the hour is 0'. Hence our user would only see a birthday message on day 120 at hour 0.
Variations
Now there are a few variations to the if statement. First of all, an if can come either with or without an else statement:
if (day == 120)
{
std::cout"happy birthday";
}
// some other code follows here...
if (day == 130)
{
std::cout"its day 130";
}
else
{
std::cout<<"its not day 130";
}
It can also come with multiple else if statements like this:
if (day == 1)
{
std::cout<<"its day one!";
}
else if (day == 2)
{
std::cout<<"its day two!";
}
else if (day == 3)
{
std::cout<<"its day three!";
}
else
{
std::cout<<"its a day after day three!"
}
Another useful feature is to put more if statements inside an if statement, like so:
if (day == 1)
{
if (hour == 0)
{
std::cout<<"its day one, hour 0";
}
else if (hour == 1)
{
std::cout<<"its day one, hour 1";
}
else if (hour == 2)
{
std::cout<<"its day one, hour 2";
}
}
Which means that only if the day equals 1 then we execute all those hour checks in there.
The Evil-If
One more thing to know about if statements is that we can write them without { and } if the code that should be executed is only exactly one line, like here:
if (day == 1)
std::cout<<"its day one!";
else
std::cout<<"its not day one!";
While that looks nice, it also prone to bugs like this one:
if (day == 1)
std::cout<<"its day one!";
std::cout<<"I am so happy!";
In the above code it looks like the two messages 'its day one' and 'I am so happy' are only shown on day one, but that's not the case. As explained before, when we don't use { and } around if statements then only the next line is part of the code that is executed if the condition is true. This means that in our case, only 'its day one' is executed if its actually the day one, and our 'I am so happy' message is executed in any case.
If we want to avoid all kinds of nasty bugs, then we just surround our code with { and } so it's obvious what's part of the if and what isn't:
if (day == 1)
{
std::cout<<"its day one!";
std::cout<<"I am so happy!";
}
Switch
To make our lives easier (or sometimes harder?), C++ gives us an alternative way for else if statements. For example, our example from above:
if (day == 1)
{
std::cout<<"its day one!";
}
else if (day == 2)
{
std::cout<<"its day two!";
}
else if (day == 3)
{
std::cout<<"its day three!";
}
else
{
std::cout<<"its a day after day three!"
}
can be written with a switch statement like so:
switch (day)
{
case 1: std::cout<<"its day one!"; break;
case 2: std::cout<<"its day two!"; break;
case 3: std::cout<<"its day three!"; break;
default: std::cout<<"its a day after day three!"; break;
}
Here is what happens in the background:
- C++ looks at the day variable
- In case it's 1 then it prints our 'its day one!' message and stops (hence break)
- In case it's 2 then it prints our 'its day two!' message and stops (hence break)
- In case it's 3 then it prints our 'its day three!' message and stops (hence break)
- Otherwise ('default') it prints our 'its a day after day three!' message and stops (hence break)
Many people tend to avoid the switch statement because it looks a bit complicated. If you like if and else if more, then feel free to use just that.