If statements allow the programmer to control what happens in the program more. You use if statements in real life all the time. Let's say you were driving a car, for instance. If a traffic light is red, then you stop. Else, you go (or keep going, for that matter).
Example: Check if a number is too large (Over 100)
Let's say with code, you want to input a value and then print out a result based on how big it is.
Code (C++)
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Number: ";
cin >> number;
if (number > 100) {
cout << "Number is too big!" << endl;
}
else {
cout << "Number is perfect!" << endl;
}
}
Outputs:
Output
Number: 25
Number is perfect!
Output
Number: 125
Number is too big!
So what exactly is going on here? Simple, the program will read in a number, and then it will check if that number is over 100 or not. If it is, then it will execute everything in between the {...} after the if statement. If the number is not over 100, it must be less than or equal to 100. Therefore, it would run whatever is after "else {".
The brackets and your indentation are very important.
Technically, we can make the same program with checking if the number is less than or equal to 100:
Code (C++)
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Number: ";
cin >> number;
if (number <= 100) {
cout << "Number is perfect!" << endl;
}
else {
cout << "Number is too big!" << endl;
}
}
Outputs:
Output
Number: 25
Number is perfect!
Output
Number: 125
Number is too big!
Example: Three or more conditions (Mutual Exclusion)
Let's extend the traffic light example. Let's say if the light was red, you stop. Okay fair enough. Otherwise... if the light was yellow, you slow down. Simple enough. If the light is green, you fly by it in stride. Now we have three conditions to consider. Let's look at it. in the form of another example. We will have a user input the colour of a traffic light and determine what happens:
Code (C++)
#include <iostream>
#include <string>
using namespace std;
int main() {
string colour;
cout << "Light Colour: ";
cin >> colour;
if (colour == "GREEN") {
cout << "Go!" << endl;
}
else
if (colour == "YELLOW") {
cout << "Slow down!" << endl;
}
else
if (colour == "RED") {
cout << "Stop!" << endl;
}
else {
cout << "What kind of light is this!?" << endl;
}
}
If we run the program a few times, we get this:
Output
Light Colour: GREEN
Go!
Output
Light Colour: YELLOW
Slow down!
Output
Light Colour: RED
Stop!
Output
Light Colour: BLUE
What kind of light is this!?
"else" is very handy in this situation. When we check for traffic light colours, if it isn't green, it will then check to see if it is yellow. If it isn't yellow, it will then check if it is red. If it isn't red, then clearly it isn't green, yellow, or red. So it must be something else. That is where we print "What kind of light is this!?".
Example: Nested If Statements
Let's branch off of the traffic light example once again. Let's say you wanted to run the red light... So far, our if statement (from above) is:
Code (C++)
...
if (colour == "RED") {
cout << "Stop!" << endl;
}
...
Let's allow the user to input whether or not they want to run the light. If they input "YES", then they will go regardless of the light being red.
Code (C++)
...
if (colour == "RED") {
string run;
cout << "The light is red! Run it? ";
cin >> run;
if (run == "YES") {
cout << "You ran the light, you horrible person." << endl;
} else {
cout << "You stopped!" << endl;
}
}
...
Do you see what is going on here? Inside of the {...} for the if statement checking if the colour is "RED", we add another input to see if the user wants to run the light. And if so, have another if statement (nested inside the first one) checking that answer.
Putting it all together now:
Code (C++)
#include <iostream>
#include <string>
using namespace std;
int main() {
string colour;
cout << "Light Colour: ";
cin >> colour;
if (colour == "GREEN") {
cout << "Go!" << endl;
}
else
if (colour == "YELLOW") {
cout << "Slow down!" << endl;
}
else
if (colour == "RED") {
string run;
cout << "The light is red! Run it? ";
cin >> run;
if (run == "YES") {
cout << "You ran the light, you horrible person." << endl;
} else {
cout << "You stopped!" << endl;
}
}
else {
cout << "What kind of light is this!?" << endl;
}
}
Outputs:
Output
Light Colour: RED
The light is red! Run it? YES
You ran the light, you horrible person.
Output
Light Colour: RED
The light is red! Run it? NO
You stopped!