Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
An if statement checks its condition before it runs any statement inside its if body. In this weather decision, isRaining has the value true, so the condition is true and the two output statements inside the braces run.
bool isRaining = true;
if (isRaining) {
cout << "Take an umbrella" << endl;
cout << "Wear boots" << endl;
} else {
cout << "Leave the umbrella" << endl;
cout << "Wear shoes" << endl;
}
cout << "Leave home" << endl;Because isRaining is true, execution enters the if body. It prints "Take an umbrella" and then "Wear boots". The condition is evaluated before either output statement runs, so the value of the condition determines which body receives control.
Given isRaining = true, which output statements run from the weather decision?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The else body does not have its own condition. It is the path selected when the if condition is false. If the same assignment changes isRaining to false, execution skips the if body and enters the else body instead.
bool isRaining = false;
if (isRaining) {
cout << "Take an umbrella" << endl;
cout << "Wear boots" << endl;
} else {
cout << "Leave the umbrella" << endl;
cout << "Wear shoes" << endl;
}
cout << "Leave home" << endl;With the false value, the weather messages are "Leave the umbrella" and "Wear shoes". An if-else pair has two possible bodies, but one evaluation of its condition selects exactly one of them. The selected body runs, and the other body does not run.
Both weather messages belong to each branch, so each pair needs braces. Braces mark the complete body. Without them, an if or else controls only the next statement, even if the following statement is indented to look as though it belongs there.
if (isRaining)
cout << "Take an umbrella" << endl;
cout << "Wear boots" << endl;
else {
cout << "Leave the umbrella" << endl;
cout << "Wear shoes" << endl;
}In the unbraced true branch, only the first output statement is controlled by if. The "Wear boots" statement is outside that body, despite its indentation. In this exact arrangement, the else also no longer attaches correctly, so the code produces a syntax error. Even when the code remains syntactically valid, misleading indentation can make an unconditional statement look conditional.
Restore braces around both output statements in the true branch.
if (isRaining)
cout << "Take an umbrella" << endl;
cout << "Wear boots" << endl;
else {
cout << "Leave the umbrella" << endl;
cout << "Wear shoes" << endl;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The decision chooses a path temporarily. With isRaining true, the if body prints its two messages, then control leaves the braces and reaches "Leave home". With isRaining false, the else body prints its two messages, then control reaches that same statement. The decision does not stop execution after the selected body.