if (expression) statement
if (a == 0) a = 1;
if (inches > 72 && weight <= 98) { beanpole_count++; kick_sand_in_face(); }
if (expression) statement-1
else statement-2
if (a != 0) ; else a = 1;
if (first_time) { initialize(); first_time = 0; } else { do_something(); }
if (first_time) initialize(); else do_something();but this:
if (first_time) initialize(); first_time = 0; else do_something();is a syntax error, as is:
if (first_time) { initialize(); first_time = 0; }; else { do_something(); }
else
statement matches the nearest previous if
statement in the same scope:
else
in this code:
if (a == b) if (b > c) a_lt_c(); else a_ge_c();matches the
if (b > c)
, even if it is written as:
if (a == b) if (b > c) a_lt_c(); else a_ge_c();
if (a == b) { if (b > c) a_lt_c(); } else a_ge_c();to push the
if (b > c)
down to a lower level of scope
if (a < c) max = c; else max = a;can also be expressed using a conditional expression, which would look like:
max = (a < c) ? c : a;
if
/else
version, a conditional expression can be used in the same way as any other
expression, and is thus a bit more versatile
while (expression) statement
not_done = 1; while (not_done) not_done = do_something();
while (a != 0) if (a > 0) a--; else a++;
do statement while (expression)
do
is similar to the while
statement except that
expression is evaluated at after statement instead of
before it
while
example would be better written as:
do { not_done = do_something(); } while (not_done);
for (init-expr; eval-expr;
incr-expr) statement
for
is the same as a while
statement:
for ( ; i > 0; ) { do_something(); }is identical to
while (i > 0) { do_something(); }and the
while
version is probably a bit more readable
for (min = max = func(0), i = 1; i < num_entries - 1; i++) { x = func(i); if (x < min) { min = x; } else if (x > max) { max = x; } }
break
while (a > 0) a--;
could also be written as
while (1) { if (a <= 0) break; else a--; }
while (i > 0) { while (j > 0) { if (j > 50) { break; } j--; } i--; }if
j
is greater than 50, the while (j > 0) { ... }
loop will be terminated and execution will resume at the i++
statement
continue
while (i > 0) { while (j > 0) { if (j > 50) { j = -100; continue; } j--; } i--; }if
j
is greater than 50, j
will be set to -100,
the j++
statement will be skipped and execution will resume
at the top of the while (j > 0) { ... }
loop
switch (int-expression) { switch-body }
switch
statement is a way of branching to one
of a number of blocks of code based on the result of int-expression
case int-constant:
or
default:
label
switch
statement is executed,
int-expression is evaluated and the value is checked
against the int-constant
argument of each case
label
case
label is executed
default:
label exists, the
code immediately following the default
label is executed
default
label exists, the
switch
statement is exited
case
or default
label should be terminated with either a break
or
return
statement or execution will "fall through" to the
next executable code
switch
example:
/* assumes ASCII character set */ int read_number(void) { int c; int total = 0; do { c = getchar(); switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': total = (total * 10) + c - '0'; break; case '\n': c = EOF; break; case EOF: break; default: return -1; } } while (c != EOF); }
break
to exit the switch
statement
case EOF
statement only does a break
,
the break
in the case '\n'
statement could have
been eliminated, causing it to fall through to the break
after
the case EOF
goto label
:
' (colon)
label1: a = 0; l_two: { third: b = 1; final: c = 2; }
}
'
empty: ; }
goto
is used to jump to a labelled statement in a function
goto downbelow; downbelow: goneto = 1;
goto