Now that we have boolean values, we need to use them to control what code gets executed. Conditional statements give us this ability. The simplest conditional statement in C++ is the if statement:
An if statement consists of if, followed by an expression in parentheses, followed by a block of code. The expression is used to determine whether the block of code should be executed.
if (x > 0) {
cout << "x is positive" << endl;
cout << "that means larger than 0";
}
Subsection7.3.1The condition
The expression in parentheses is called the condition and must be a variable or expression with a boolean value. If it is true, the statements in the curly braces get executed. That group of statements in the braces is known as the body. If the condition is false, execution skips over the body.
Try running this sample in Codelens. Notice that only one of the three conditions is true and that the bodies of the other two conditionals get skipped.
The condition can be any value that evaluates to true or false. Because numbers can evaluate as true or false (with 0 being false and all other numbers being true), that means it is possible, but generally not advisable, to write something like this:
int x = 5;
bool isEven = (x % 2 == 0);
if (isEven) {
cout << "x is even" << endl;
}
This way of testing a boolean variable is generally preferred to comparing it to true. Writing if (isEven == true) would do the same thing, but is redundant.
A common mistake is to use a single = instead of a double == when comparing values. If you make this mistake, the compiler will emit a warning like the one shown below that tells you you are using an assignment operator in a place where a true/false value is expected.
The body is the code that gets conditionally. Although above we have shown the body in braces ({ }), those are not strictly required if the body is only a single statement:
if (x > 0) {
cout << "x is positive" << endl;
}
if (x > 0)
cout << "x is positive" << endl;
The braces serve to define a block that groups multiple statements into a single one. If we want to have two statements that are both part of the body of the if, they must be made into a block with { }:
// legal, but not recommended
if (x > 0) {
cout << "x is positive" << endl;
cout << "that means larger than 0" << endl;
}
By convention, we indent the code that is inside a condition, whether or not it is inside braces, to clearly show that it is βinsideβ the body of the if. The compiler does not care if you indent or not, but indenting blocks makes code much easier to read.
Because indentation signals βthis is insideβ to programers, but it meaningless to the compiler, it can be dangerous to skip braces, even when they are optional. Doing so makes it easy to accidentally add a statement to a one-line if block.
With a modern compiler, you likely will get a warning about that code. But it is possible to silence the warning (like we have here) and run the code anyway.
The indentation in the first version was misleading. The second print statement is not a part of the body, and as a result, it runs no matter what. Even experienced programmers make this mistake; search the web for Appleβs βgoto failβ bug. To avoid this, it is a good idea to always use braces, even for one line bodies.
In all previous examples, notice that there is no semicolon at the end of the line with if (condition). That is because the body is technically part of the same statement. This is valid C++:
But the strong convention is to write the body on a line by itself. However, it is all still one statement and there should not be a semicolon at the end of the line with the if condition. If you put a semicolon after the condition, like this:
int n = 16;
int x = 4;
bool evenFlag = (n % 2 == 0);
bool plusFlag = (x > 0);
if (evenFlag) {
cout << "n was even when I checked it ";
}
if (plusFlag) {
cout << "x was positive when I checked it";
}
n was even when I checked it x was positive when I checked it