C Now

C Statement Types

Site Logo
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.

C Statement Types (for Unstructured Programming)
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) {
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

C Translation Unit Syntax  C Translation Unit Syntax
C Declaration Syntax  C Declaration Syntax
C Statement Syntax  C Statement Syntax
C Expression Syntax  C Expression Syntax
C Now Home  C Now Home
Syntax Index  Syntax Index
E-Mail
Valid HTML 4.01 Transitional Valid CSS! Site Logo

copyright 1999-2006, j.h.young, revised 2/13/06

C Translation Unit Syntax / C Declaration Syntax / C Statement Syntax / C Expression Syntax /