CKTIS / C
else — the alternative branch of if
The `else` keyword introduces the branch executed when the associated `if` condition compares equal to zero. It has no condition of its own and cannot stand alone. Braces help make its association with an `if` unambiguous.
Version: C23; repository compiler flag, production runtime not attested
Syntax
if (expression) {
/* true branch */
} else {
/* false branch */
}else is an optional part of an if statement. On ordinary entry to that statement, the condition is evaluated: a result unequal to 0 selects the first branch, while a result equal to 0 selects the branch after else. The condition must have scalar type, such as an arithmetic or pointer type; it need not have type bool.
else takes no condition of its own. Writing else if (...) places another if statement in the else branch; it does not form a separate keyword. This builds a chain of checks, with a final else handling the case where none of the earlier conditions was satisfied.
Each else associates with the nearest preceding if permitted by the syntax, regardless of indentation. This is the “dangling else” pitfall. Use { } to make branch boundaries clear and group multiple statements. Do not add a semicolon after the closing } of the if branch before else: it would separate elements that must form one statement.
In the example, score >= 50 produces 0 because score is 42. Consequently, only puts("retry") runs, and the expected output is retry followed by a newline. This explanation follows draft N3096, § 6.8.4.1; it does not attest behavior in a production runtime.
Examples
#include <stdio.h>
int main(void)
{
int score = 42;
if (score >= 50) {
puts("pass");
} else {
puts("retry");
}
return 0;
}
Result
retry
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.