# A Little C Primer/C Control Constructs

C contains a number of looping constructs, such as the "while" loop:
If "test" starts with an initial value less than or equal to 0 the "while" loop will not execute even once. There is a variant, "do", that will always execute at least once:
The most common looping construct is the "for" loop, which creates a loop much like the "while" loop but in a more compact form:
Notice that with all these loops, the initial loop statement does not end with a ";". If a ";" was placed at the end of the "for" statement above, the "for" statement would execute to completion, but not run any of the statements in the body of the loop.
The "for" loop has the syntax:
All the elements in parentheses are optional. A "for" loop could be run indefinitely with:
—although using an indefinite "while" is cleaner:
It is possible to use multiple expressions in either the initialization or the modifying expression with the "," operator:
The conditional tests available to C are as follows:
The fact that "==" is used to perform the "equals" test, while "=" is used as the assignment operator, often causes confusion and is a common bug in C programming:
C also contains decision-making statements, such as "if":
This example can be more easily implemented using an "else" clause:
Since there is only one statement in each clause the curly brackets aren't really necessary. This example would work just as well:
However, the brackets make the structure more obvious and prevent errors if you add statements to the conditional clauses. The compiler doesn't care one way or another, it generates the same code.
There is no "elseif" keyword, but "if" statements can be nested:
This is somewhat clumsy. The "switch" statement does a cleaner job:
The "switch" statement tests the value of a single variable; unlike the "if" statement, it can't test multiple variables. The optional "default" clause is used to handle conditions not covered by the other cases.
Each clause ends in a "break", which causes execution to break out of the "switch". Leaving out a "break" can be another subtle error in a C program, since if it isn't there, execution flows right through to the next clause. However, this can be used to advantage. Suppose in our example the routine can also be asked to fire a ROCKET, which is the same as a MISSILE:
The "break" statement is not specific to "switch" statements. It can be used to break out of other control structures, though good program design tends to avoid such improvisations:
If the "for" loop were nested inside a "while" loop, a "break" out of the
"for" loop would still leave you stuck in the "while" loop. The "break" keyword only applies to the control construct that executes it.
There is also a "continue" statement that skips to the end of the loop body and continues with the next iteration of the loop. For example:
Finally, there is a "goto" statement:
—that jumps to an arbitrary tag within a function, but the use of this
statement is generally discouraged and it is rarely seen in practice.
While these are the lot of C's true control structures, there is also a special "conditional operator" that performs a simple conditional assignment of the form:
—using a much tidier, if more cryptic, format:
the ?: construct is called a ternary operator—or the ternary operator—as it takes 3 arguments.