C++ Notes: Control Flow Statements

The control statements contain one logical statement, which is usually a block of statements enclosed in curly braces {}.

Indenting control structures is essential to making readable programs. There are several styles. The style I use is the common industrial or K&R style. The openning curly brace is on the end of the beginning statement. The idea is to avoid creating too much visual white space separating the parts of the statement. Indentation is usually best with 4 spaces, altho 3 and 2 are also used.

True/false values are used in the if and loop statements. These are either the Boolean values true and false, or any non-zero value for true and zero for false (as in C).

Selection (if, switch)

if Statement

//----- if statement with a true clause
   if (expression) {
       statements // do these if  expression is true
   }
   
//----- if statement with true and false clause
   if (expression) {
       statements // do these if expression is true
   } else {
       statements // do these if expression is false
   }
   
//----- if statements with many parallel tests
   if (expression1) {
       statements // do these if expression1 is true
   } else if (expression2) {
       statements // do these if expression2 is true
   } else if (expression3) {
       statements // do these if expression3 is true
   . . .
   } else {
       statements // do these no expression was true
   }
   
   

switch Statement

The switch statement chooses statements to execute depending on an integer value. The same effect can be achieved with a series of cascading if statements, but in some cases the switch statement is easier to read, and in some compilers it can produce more efficient code. The break statement exits from the switch statement. If there is no break at the end of a case, execution continues in the next case, but this is almost always an error.
  switch (expr) {
      case c1:
            statements // do these if expr == c1
            break;
            
      case c2: 
            statements // do these if expr == c2
            break;
            
      case c2:  // Multiple values can share same code.
      case c3:
      case c4:
            statements // do these if expr ==  any of c2, c3, or c4.
            break;
      . . .
      default:
            statements // do these if expr != any above
  }
Loop Statements

while Statement

The while statement tests an expression. If the expression evaluates to true, it executes the body of the while. If it is false, execution continues with the statement after the while body. Each time after the body is executed, execution starts with the test again. This continues until the expression is false or some other statement (break or return) stops the loop.
  while (testExpression) {
      statements
  }

for Statement

Many loop have an initialization before the loop, and some "increment" before the next loop. The for loop is the standard way of combining these parts.
  for (initialStmt; testExpr; incrementStmt) {
      statements
  }
This is the same as:
   initialStmt;
   while (testExpr) {
       statements
       incrementStmt
   }
   

do Statement

This is the least used of the loop statements, but sometimes you want a loop that executes one time before testing.
   do {
       statements
   } while (testExpression);

Other loop controls

break;    // exit innermost loop or switch
continue; // start next innermost loop iteration

Function Return

return;      //return no value from void function
return expr; //return value of expr from function