Statements
Expression statement
expression;
The expression is executed for its side effects,
if any (such as assignment or function call).
Compound statement
{
declaration-list[opt]
statement-list[opt]
}
-
Delimited by { and }.
-
May have a list of declarations.
-
May have a list of statements.
-
May be used wherever statement appears below.
Selection statements
if
if (expression)
statement
-
If expression evaluates to nonzero (true), statement is executed.
-
If expression evaluates to zero (false),
control passes to the statement following statement.
-
The
expression
must have scalar type.
else
if (expression1)
statement1
else if (expression2)
statement2
else
statement3
-
If expression1 is true,
statement1
is executed, and control passes to the statement following
statement3.
Otherwise,
expression2 is evaluated.
-
If expression2 is true,
statement2
is executed, and control passes to the statement following
statement3.
Otherwise,
statement3
is executed, and control passes to the statement following
statement3.
-
An else is associated with the lexically nearest
if that has no else and that is at the same block level.
switch
switch (expression)
statement
-
Control jumps to or past
statement
depending on the value of
expression.
-
expression must have integral type.
-
Any optional case is labeled by an integral
constant expression.
-
If a default case is present, it is executed if no
other case match is found.
-
If no case matches, including default,
control goes to the statement following statement.
-
If the code associated with a
case
is executed,
control falls through to the next case unless a
break statement is included.
-
Each case of a switch must have a unique constant
value after conversion to the type of the controlling expression.
In practice,
statement
is usually a compound statement with
multiple cases, and
possibly a default;
the description above shows the minimum usage.
In the following example, flag gets set to 1
if i
is 1 or 3, and to 0 otherwise:
switch (i) {
case 1:
case 3:
flag = 1;
break;
default:
flag = 0;
}
Iteration statements
while
while (expression)
statement
This sequence is followed repetitively:
-
expression is evaluated.
-
If expression is non-zero, statement is executed.
-
If expression is zero, statement is not executed,
and the repetition stops.
expression
must have scalar type.
do-while
do
statement
while (expression);
This sequence is followed repetitively:
-
statement is executed.
-
expression
is evaluated.
-
If
expression
is zero, repetition stops.
(do-while
tests loop at the bottom;
while
tests loop at the top.)
for
for (clause1; expression2; expression3)
statement
-
clause1 initializes the loop. It can be a declaration (with
scope through to the end of the loop) or an expression.
-
expression2 is tested before each iteration.
-
If expression2 is true:
-
statement is executed.
-
expression3 is evaluated.
-
Loop until expression2 is false (zero).
-
Any of expression1, expression2,
or expression3 may be omitted, but
not the semicolons.
-
expression1
and
expression3
may have any type;
expression2
must have scalar type.
Jump statements
goto
goto identifier;
-
Goes unconditionally to statement labeled with identifier.
-
Statement is labeled with an identifier followed by a colon, as in:
A2: x = 5;
-
Useful to break out of nested control flow statements.
-
Can only jump within the current function.
break
Terminates nearest enclosing switch,
while, do, or for statement.
Passes control to the statement following the terminated statement.
Example:
for (i=0; i<n; i++) {
if ((a[i] = b[i]) == 0)
break; /* exit for */
}
continue
Goes to top of smallest enclosing
while, do, or for statement,
causing it to reevaluate the controlling expression.
A for loop's expression3 is evaluated
before the controlling expression.
Can be thought of as the opposite of the
break
statement.
Example:
for (i=0; i<n; i++) {
if (a[i] != 0)
continue;
a[i] = b[i];
k++;
}
return
return;
return expression;
-
return
by itself exits a function.
-
return
expression
exits a function and returns the value of
expression.
For example,
return a + b;
Previous topic:
Initialization
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004