Control Flow
Programs execute statements in order, top to bottom. Control flow statements change that order: they let you choose between paths, repeat blocks, or select among many options. This chapter covers the three primary control flow mechanisms -- conditionals, iterations, and multiple selection -- in both C and Auto.
3.1 Conditional Execution
The most basic control flow construct is the conditional: do this, or do that, depending on a condition.
C's if/else
In C, if evaluates an expression. If the result is nonzero, the first branch executes. Otherwise, the else branch (if present) executes:
if (x > 0) {
printf("positive\n");
} else if (x < 0) {
printf("negative\n");
} else {
printf("zero\n");
}The braces around the body are optional for single-statement branches, but Modern C style always uses them. This prevents a class of bugs where a maintenance edit adds a second statement but forgets to add braces.
Auto's if/else
Auto uses the same logical structure, but without parentheses around the condition:
if x > 0 {
print("positive")
} else if x < 0 {
print("negative")
} else {
print("zero")
}Auto requires braces for all branches. There is no "optional braces" rule. This eliminates the dangling-else ambiguity and the single-statement brace-forgetting bug entirely.
Truth and Falsehood
In C, truth is simple: zero is false, everything else is true. This applies to all scalar types:
if (42) // true
if (0) // false
if (3.14) // true
if (0.0) // false
if (ptr) // true if ptr is not NULL
if (!ptr) // true if ptr is NULLAuto inherits the same model. A nonzero value is truthy; zero is falsy. The bool type provides explicit boolean values true and false, but any condition expression works as in C.
C Deep Dive: In C, every scalar value has a truth interpretation: zero is false, nonzero is true. This means
if (ptr),if (count), andif (difference != 0)are all valid conditions. Modern C's advice is explicit: "Don't compare to 0, false, or null." But this is a stylistic choice, and many C programmers prefer the explicit comparison for clarity. Auto does not change this -- the same truth model applies.