C Statement Types (for Structured Programming)
Type |
Syntax | Example | Semantics |
null |
; | ; |
Does nothing semantically |
expression |
<expression> ; | x=4; |
The <expression> is evaluated |
compound |
{ <statement list> } |
{x=4; y='A';} |
The <statement list> is executed in sequence. |
while |
while ( <expression> ) <statement> |
while (x < 10) x += 1; |
The <statement> is executed as long as the
<expression> is true (non-zero). |
do |
do <statement> while ( <expression> ); |
do a[i] = b[i] while (i < 10); |
The <statement> is repeated as long as the
<expression> is true (non-zero). |
for
| for ( <expression 1>; <expression 2>; <expression 3>) <statement> |
for (i = 0; i < 10; ++i)
a [i] = b [i];
|
Equivalent to:<expression 1>;
while (<expression 2>) {
<statement>
<expression 3>;
} |
return
| return <expression>;
| return status;
| Control is returned to the calling statement,
and the <expression> is evaluated,
and its value is returned as the value of the function.
|
---|
return;
| return;
| Control is returned to the calling statement.
This form of the return statement can only
be used within a function having a void (return) type.
|
if
| if ( <expression> )
<statement>
| if (x < 0) x = -x;
| The <expression> is evaluated.
If it is true (non-zero), the <statement> is executed.
|
---|
if-else
| if ( <expression> )
<statement 1>
else
<statement 2>
| if (x < 0) x = -x; else y = 4;
| The <expression> is evaluated.
If it is true (non-zero), the <statement 1> is executed.
If it is false (zero), the <statement 2> is executed.
|
---|
switch
| switch ( <expression> )
( <case list> }
where <case list>
is a sequence of case <value>: <statement list>
break;
and optionally one default: <statement list>
break;
| switch (key) { case 'A': state = 1;
break;
case 'B': state = 2;
break; default: state = 0;
break;
}
| The <expression> is evaluated.
Its value is then compared to each case <value>.
If it is equal, the <statement list>
following that case is executed.
(Each value must appear at most once.)
If none of the case <value>s is equal to
the expression value,
the <statement list> following
a default: is executed. |