Chapter 5
Statements

Present in C are the usual structures to control the flow of execution of a program, also found in many other programming languages. There are branching structures, iteration structures, labels and jumps, and a few more.

Statements are executed one after the other in the order in which they are specified, and they tell the computer what actions to perform in sequence. It is this sequence of instructions that determines the outcome of a program and which turns programming into art.

15 Blocks

A block is just a sequence of statements enclosed in braces.

{
  statement-1;
  statement-2;
  statement-3;
  ⋮
}

It is used anywhere a single statement is expected, but when more than one statement must be executed in sequence. We'll see examples of its use shortly. A block may occur anywhere inside a function.

16 Declarations

A statement may be a declaration, which we saw elsewhere how to write. They must end with a semicolon.

declaration;

Here follows an example.

const int kong_a = 1, kong_b = 2, kong_c = 3;

We are declaring three constants here. There follows the declaration of two variables, which we'll be using throughout this chapter to illustrate some examples.

int kong, bananas;

17 Expressions

Expressions, which we met previously, are also, by themselves, statements. We must append a semicolon at the end, as in

expression;

An example is

bananas--;

This statement decrements the value of bananas by 1.

18 ifelse

ifs help the programmer make decisions. An if statement is

if(expression)
  statement-true;
else
  statement-false;

or the simpler

if(expression)
  statement-true;

with no dangling else. First, expression is evaluated. If it is not 0 (that is, not false), then statement-true is executed. Otherwise, if there is a dangling else, then statement-false is executed. statement-true and statement-false may each be blocks, if more than one instruction is involved in each case. For example,

if(has_bananas)
{
  bananas--;
  printf("One banana was eaten!\n");
}
else
  printf("No more bananas to eat!\n");

Here, we first check whether has_bananas is not equal to false. If so, we decrement bananas by 1 and display the message "One banana was eaten!\n". If, on the other hand, has_bananas equals false, we print the message "No more bananas to eat!\n". Note the use of a block with two statements in the statement-true part of this if.

19 switch

A more comprehensive decision structure is the switch statement, which has the format

switch(expression)
{
  case constant-1:
    statements-1;
    break;
  case constant-2:
    statements-2;
    break;
  ⋮
  case constant-n:
    statements-n;
    break;
  default:
    statements-default;
}

expression is evaluated and it has to be an integer. If it equals constant-1, which must be a constant literal, then statements-1 are executed. If expression is constant-2, then statements-2 are executed. This process continues until all cases are exhausted. If no constants match expression, then statements-default are executed. The statements don't have to be inside of blocks.

break is used to tell the computer to continue execution after the end of the switch. If a case has no break before the next case, then execution continues with the statements of the next case. We say execution falls through to the next case.

For example,

switch(kong)
{
  case kong_a:
    eat_banana(kong_a);
    break;
  case kong_b:
    eat_banana(kong_b);
    break;
  case kong_c:
    eat_banana(kong_c);
    break;
  default:
    printf("Unknown Kong!\n");
}

Here, if the integer variable kong is the constant kong_a, then the call eat_banana(kong_a) is made and the switch is exited. If it is kong_b or kong_c, then an analogous procedure is made. If it is neither kong_a, nor kong_b, nor kong_c, then the message "Unknown Kong!\n" is printed.

Here is an example with some breaks omitted.

switch(kong)
{
  case kong_a:
  case kong_b:
  case kong_c:
    eat_banana(kong);
    printf("Banana eaten!\n");
    break;
  default:
    printf("Unknown Kong!\n");
}

If the integer variable kong is one of the constants kong_a, kong_b, or kong_c, then that Kong eats a banana, the message "Banana eaten!\n" is printed, and the switch is exited. Otherwise, the message "Unknown Kong!\n" is printed.

20 Labels and goto

A label is simply an identifier followed by a colon, before a statement, as in

label: statement;

Elsewhere, we may send the flow of execution of the program to this label with the help of the goto command.

goto(label);

Execution then continues from the statement following the label. Programmers should not jump to a label from outside its block. Here is an example.

bananas = 10;
cycle: if(bananas != 0)
{
  bananas--;
  goto(cycle);
}
else
  printf("No more bananas!\n");

This decrements the variable bananas until it reaches 0. After that, it prints the message "No more bananas!\n".

21 while

Next comes an iteration structure, and it allows the programmer to repeat some actions until some condition occurs. It is the while statement, and it has the format

while(expression)
  statement;

First, expression is evaluated. If it is not 0, then statement is executed. Then, expression is evaluated again, and statement is executed again, if expression is not 0. This cycle repeats itself until expression is 0.

Here is an example.

while(bananas != 0)
{
  bananas--;
  printf("One banana was eaten!\n");
}

To begin with, we check the value bananas has. If it is not 0, then we decrement it and print the message "One banana was eaten!\n". This cycle repeats itself until bananas is 0, when execution continues after the end of the while.

22 dowhile

This iteration structure is very similar to the previous one, except that the condition is tested after the statements of the cycle are executed. The general format is as follows.

do
  statement;
while(expression);

First, statement is executed. Then expression is evaluated. If it is not 0, then the cycle repeats itself. Otherwise, the cycle is exited. For example,

kong = kong_c;
do
{
  eat_banana(kong);
  kong--;
}
while(kong != 0);

We begin by initializing the variable kong to kong_c (both integers - see above). Then, that Kong eats a banana, and we move on to the previous Kong. We repeat until kong is 0. Note that all 3 Kongs eat a banana each.

23 for

The cycle for is the last of the iteration structures of C. It has the format

for(declaration; expression-test; expression-next)
  statement;
and is equivalent to
declaration;
while(expression-test)
{
  statement;
  expression-next;
}

declaration initializes the variable that is going to be used to control the flow of the cycle. If expression-test is not 0, then the cycle continues. Otherwise, it stops, and execution continues with the statement after its end. expression-next updates the value of said variable. Here is an example.

for(int i = 1; i < 4; i++)
  eat_banana(i);

This makes each Kong eat a banana, and achieves the same as

eat_banana(1);
eat_banana(2);
eat_banana(3);

Note that the variable i declared inside the for is local to the cycle and cannot be used outside of it, unless it it redeclared again outside of the cycle.

24 More Statements

We end this chapter with the statements return, continue, and break.

return;

or

return expression;

immediately exits a function. If that function returns a value, the latter form is used. The statement

continue;

can only be used inside a loop, and it jumps to the next test of the loop. For example,

for(int i = kong_a; i <= kong_c; i++)
  if(i != kong_b)
    continue;
  else
    eat_banana(i);

This is the same as

eat_banana(kong_b);

The three iterations of the cycle are executed, but eat_banana(i) is only executed when i is kong_b. In all other cases, the next iteration of the cycle is executed with the use of continue. Finally, comes

break;

which we already saw in the switch section. It simply jumps out of a loop or switch statement. Here is an example of an infinite cycle and how we may leave it.

for(;;)
  if(bananas != 0)
    bananas--;
  else
    break;