Control flow

We have seen several examples of control flow statements. The simplest is the if statement:

if( x < 13 )
  delay( x );

We can also add an else statement after it:

if( cost < bank_balance )
  buy_it();
else
  walk_away();
​

Another simple control flow statement is the while:

while( ++day < DAYS_PER_WEEK )
  salary += ONE_DAY_PAY;
​

A little more complicated is the for statement, which has three parts, the initialization, the condition, and the loop expression (where the control variable is usually modified):

for( int a = 0; a < HOW_HIGH; ++a )
  jump();
​

All three parts are optional -- the most reduced form is for(;;), which means to loop forever.

If we want to group several statements together so they can be controlled by an if, a while, or a for, then we simply put curly braces around the group of statements:

if( today == TUESDAY )
{
   do_the_laundry();
   walk_the_dog();
   call_mom();
}
​

Comments are any text after two forward slashes. Everything from the slashes to the end of the line is ignored by the compiler.

Statements end in a semicolon, as you have seen in all of the examples in this book.