Controlling the flow of your program
Ultimately, what we want is the code to branch in one way under certain conditions. Code commands that change which line of code gets executed next are called control flow statements. The most basic control flow statement is the if
statement. To be able to code if
statements, we first need a way to check the value of a variable.
So, to start, let's introduce the ==
symbol, which is used to check the value of a variable.
The == operator
In order to check whether two things are equal in C++, we need to use not one but two equal signs (==
) one after the other, as shown here:
int x = 5; // as you know, we use one equals sign int y = 4; // for assignment.. // but we need to use two equals signs // to check if variables are equal to each other cout << "Is x equal to y? C++ says: " << (x == y) << endl;
If you run the preceding code, you will notice that the output is this:
Is x equal to y? C++ says
: 0
In C++, 1 means true, and 0...