|
C Now |
C Statement Types |
![]() |
| 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> ) |
while (x < 10) |
The <statement> is executed as long as the <expression> is true (non-zero). |
| do |
do <statement> |
do a[i] = b[i] |
The <statement> is repeated as long as the <expression> is true (non-zero). |
| for |
for ( <expression 1>; |
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) |
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) |
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) { |
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. |
| Type | Syntax | Example | Semantics |
|---|---|---|---|
| labeled |
<label>: <statement> |
loop: x = x + 1; |
Provides a placemarker for goto statements. |
| go to |
goto <label>; |
goto loop; |
Control is transferred to the statement with the designated label. |
| break |
break; |
break; |
Control is transferred to the statement following the compound statement within which the break appears. |
| continue |
continue; |
continue; |
Control is transferred to the point following the last statement within the compound statement within which the continue appears. |
| switch |
switch ( <expression> ) ( <case list> }where <case list>is a sequence of case <value>: <statement list>and optionally one
default: <statement list>
|
switch (key) { |
The
<expression>is evaluated. Its value is then compared to each case <value>. If it is equal, control is transferred to the <statement list>following that case. (Each value must appear at most once.) If none of the case <value>s is equal to the expression value, control is transferred to the <statement list>following a default:. |
|
||||||||||||
|
|
|||||||
|
copyright 1999-2006, j.h.young, revised 2/13/06 |
C Translation Unit Syntax / C Declaration Syntax / C Statement Syntax / C Expression Syntax /