if, else and initializers in if
What you'll learn:
- choose which code runs with
if,else ifandelse; - see which
ifanelsebelongs to and avoid the danglingelse; - declare a variable right inside an
ifand know where it is visible; - pick a branch by value with
switchwithout forgettingbreak.

Picture a fork in the road with a signpost. It says: if it's raining, go left to the bus stop; otherwise, go right through the park. You read the condition and take one road, and this time you don't walk the other. A program needs forks like this too: print one thing or another, calculate one way or another.
The statements in main run in order, from top to bottom. An if statement runs the block in curly braces only when the condition in parentheses is true. The block after else runs when the condition is false:
#include <iostream>
int main() {
int t = 25;
if (t < 0) {
std::cout << "frost\n";
} else if (t < 20) {
std::cout << "cool\n";
} else {
std::cout << "warm\n"; // warm
}
}Let's walk through the program:
int t = 25;: the variabletis25.if (t < 0): the condition is false, so the program skips the first block.else if (t < 20): this condition is false too.else: the last block remains, and the program printswarm.
Branches and their order
Each such block is called a branch. An else if chain checks the conditions from top to bottom and runs the first branch whose condition is true, skipping the rest. So the order of the branches matters: the second branch is checked only when t >= 0, once the first condition is already false.
The else branch comes last and runs when every condition above it is false. Without it, when every condition is false, no branch runs.
Here the fork differs from a real road. A traveller may pick the best-fitting sign, while a program reads the signs strictly from top to bottom and takes the first one that fits.
Dangling else
Curly braces around a one-statement branch may be left out. But when two if statements are nested without braces, a reader may think the else belongs to the other if. The compiler attaches an else to the nearest if that has no else yet, and it ignores indentation:
int x = 5;
if (x > 0)
if (x > 10)
std::cout << "big\n";
else // ✗
std::cout << "not positive\n"; // not positiveThe indentation suggests that the else belongs to if (x > 0), but it actually goes to if (x > 10). Let's see what happens when x is 5:
if (x > 0): the condition is true, so the program enters the innerif.if (x > 10): the condition is false, so itselseruns.- The program prints
not positive, even thoughxis positive.
Such an else is called a dangling else. Clang and GCC warn about it, but the code compiles. Interviewers like to show this example and ask what the program prints.
Put curly braces around every branch, even a one-line one: then the compiler and the reader see the same branches.
if with an initializer
A variable needed only for the check can be declared right inside the if, before the condition, followed by a semicolon. This part of the if is called an initializer. A variable declared in it is visible in the condition and in every branch of that if:
int n = 2023;
if (int r = n % 4; r == 0) {
std::cout << "divisible by 4\n";
} else {
std::cout << "remainder " << r << '\n'; // remainder 3
}After the if, the variable r no longer exists: code that uses it below does not compile. So further down in the same function you can declare another variable named r.
Common mistake
A second line with the same indentation is added to a branch without curly braces:
int x = -1;
if (x > 0)
std::cout << "positive\n";
std::cout << "checked\n"; // ✗warning: misleading indentation; statement is not part of the previous 'if' [-Wmisleading-indentation]Without braces, an if controls only the one statement that follows it, and indentation does not change that. So checked is printed for any x, including x = -1. The compiler only warns and builds the program. The fix is curly braces around both lines.
Key points
- An
if/else if/elsechain runs the first branch whose condition is true and skips the rest. - An
elsebelongs to the nearestifwithout its ownelse, and indentation does not change that. - Without curly braces an
ifcontrols only one statement, so every branch gets braces. - A variable from an
ifinitializer is visible in the condition and in every branch, but not after theif.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.