Micron Document
The C Programming Language, Ansi C
3.1 Statements and Blocks
Chapter 3 - Control Flow

The control-flow of a language specify the order in which computations are
performed. We have already met the most common control-flow constructions in
earlier examples; here we will complete the set, and be more precise about
the ones discussed before.

An expression such as

x = 0

or

i++

or

printf(...)

becomes a

statement

when it is followed by a semicolon, as in

x = 0;
i++;
printf(...);

In C, the semicolon is a statement terminator, rather than a separator as it
is in languages like Pascal.

Braces { and } are used to group declarations and statements together into a compound statement, or block, so that they are syntactically equivalent to a single statement. The braces that surround the statements of a function are one obvious example; braces around multiple statements after an if, else, while, or for are another. (Variables can be declared inside any block; we will talk about this in Chapter 4.) There is no semicolon after the right brace that ends a block.

The

if-else

statement is used to express decisions. Formally the syntax
is

if (

expression

)

statement

1

else

statement

2

where the

else

part is optional. The

expression

is evaluated;
if it is true (that is, if

expression

has a non-zero value),

statement

1

is executed. If it is false (

expression

is zero) and if there is an

else

part,

statement

2

is executed instead.

Since an if tests the numeric value of an expression, certain coding shortcuts are possible. The most obvious is writing

if (

expression

)

instead of

if (

expression

!= 0)

Sometimes this is natural and clear; at other times it can be cryptic.

Because the else part of an if-else is optional,there is an ambiguity when an else if omitted from a nested if sequence. This is resolved by associating the else with the closest previous else-less if. For example, in