CKTIS / C++
else — the alternative branch of an if statement
The else keyword introduces an optional branch of an if statement, selected when the condition is false. The spelling else if nests another if; braces make branch association explicit.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
if (condition) {
// true branch
} else {
// false branch
}else introduces an optional alternative to if. During ordinary condition-based execution, the if branch runs when the condition is true, or the else branch runs when it is false—not both. else itself has no condition and cannot stand alone.
Each branch of an ordinary if contains one statement; a block { ... } groups multiple statements. The spelling else if is not a separate keyword: it is another if used as the statement following else.
Without braces, else binds to the nearest if that does not already have an else, regardless of indentation. Use braces to avoid mistakes in nested code. Do not insert a semicolon between the closing brace of the if branch and else.
The example tests score: the first condition is false and the second is true, so it prints pass followed by a newline. The final branch does not execute.
This entry targets C++23. The supplied site publishes an evolving working draft, not a frozen C++23 text; a repository compiler flag alone does not attest the production runtime.
Examples
#include <iostream>
int main() {
const int score = 65;
if (score >= 90) {
std::cout << "excellent\n";
} else if (score >= 50) {
std::cout << "pass\n";
} else {
std::cout << "fail\n";
}
}
Result
pass
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.