Tripod C Now
Statement Types
November 25, 1999
John's home page

C Statement Types (for Structured Programming)
Type SyntaxExampleSemantics
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.

C Statement Types (for Unstructured Programming)
Type SyntaxExampleSemantics
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) {
case 'A': state = 1; case 'B': state = 2;
default: state = 0; }
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:.

Notes:
  1. There is no assignment statement as such.  An expression containing the = operator produces assignment as a side effect.
  2. There are no input or output statements as such.  Input and output are done through function calls, as part of expressions.
  3. For structured programming, exactly one return statement should be used within each function.  It should be the last statement in the function.
  4. The <statement> of if, if-else, while, do, and for statements is usually a compound statement.

C Now C Reference Index
C Now Home Page