With various control structures, the order of execution can be different from the sequential order.
There are two major types of non-sequential control structures: selections and loops. This chapter introduces the former, and the next chapter describes the latter.
Control structures are usually used with compound statements. A compound statement is a group of statements between a pair of braces "{ }", and can be seen as a single "macro statement". The body of a function is such a compound statement.
A condition can be specified by an operator between two (variable or constant) values. The following list includes the operators:
Operator | Meaning | Type |
< | less than | relational |
> | greater than | relational |
<= | less than or equal to | relational |
>= | greater than or equal to | relational |
== | equal to | equality |
!= | not equal to | equality |
For example, given the following identifiers and their values:
x power MAX_POW y item MIN_ITEM mon_or_dad num SENTINEL -5 1024 1024 7 1.5 -999.0 'M' 999 999then the values of the following conditions can be determined:
Condition Value x <= 0 1 power < MAX_POW 0 x >= y 0 item > MIN_ITEM 1 mon_or_dad == 'M' 1 num != SENTINEL 0The above simple conditions can be combined into complicated conditions, or logical expressions, using the following logical operators: "&&"(and) "||" (or), "!" (not). IF P and Q are (simple or complicated) conditions, P||Q is true if and only if at least one of the two is true, P&&Q is true if and only if both of the two are true, and !P is true if and only if P is false.
An operator's precedence determines its order of evaluation. In the following, we list the C operators we covered so far, from the highest precedence to the lowest:
function call ! + - (unary) * / % + - < <= > >= == != && || =Among the operators of the same precedence, the execution order is from left to right, except for the unary operators what are applied from right to left. Parentheses "( )" can be used to change the order of execution.
Whenever possible, C only evaluates part of a logical expression. For example, P||Q is true if P is true, and P&&Q is false if P is false. In both cases, the second operand does not need to be evaluated. This technique is called "short-circuit evaluation".
Beside numbers, characters in C can also be compared by the relational/equality operators, using the alphabetic order. It is system dependent whether an upper-case letter is smaller than a lower-case one.
if (condition) statementT else statementFIn this structure, the "else" part is optional. FIGURE 4.4 gives the two corresponding flowcharts.
The program is in FIGURE 4.5.
A program should work correctly in any condition.
Analysis leads to the structure chart in FIGURE 4.7:
The green arrows in the chart indicate data flow, that is, arguments if the sub-problem is processed by a separate function.
The complete program is in FIGURE 4.8, which contains four functions beside the main function.
A new problem is often a variation of an old problem. In that case, to modify the old program may be better than to write a new program.
The function for this calculation is in FIGURE 4.10. The other part of the program should be modified accordingly.
When two or more if statements are put together, the order usually matters. For example, in FIGURE 4.11, the latter conditions all assume the previous conditions are false.
Nested if statements can be used to handle multiple variables, with one if statement for each variable. An example is in FIGURE 4.12 — when the road is "slick", it can either be "icy" or just "wet", depending on temperature.
C associates an else with the most recent incomplete if. Braces should be used to match an else with an earlier if.
switch (expression) { case c_1: action a_1; break; ... ... case c_n: action a_n; break; default: default_action; }In this statement, multiple cases can be combined, as well as multiple actions for the same case.
For example, the switch statement in FIGURE 4.13 allows the user to use a single letter to represent an option.
Do not confuse "==" with "=". Condition (x = 10) will always be true, because the value of an assignment is the value assigned, which is non-zero in this case.
Use braces with nested if statements, so that the else part will match the condition properly. Also, use indention will improve readability.
Don't forget to put break between cases in a switch statement.