Flow Control in C++ Programming

Part of the C++ for Dummies Cheat Sheet

The following C++ structures direct the flow of control through the program. If you're an experienced programmer, the function of these structures will be familiar from other languages.

IF

The following command evaluates booleanExpression. If it evaluates to true, then control passes to expressions1. If not, then control passes to the optional expressions2.

if (booleanExpression)
{
    expressions1;
}
[else
{
    expressions2;
}]

WHILE

The following command evaluates booleanExpression. If this evaluates to true, then control passes to expressions. At the end of the block, control passes back to booleanExpression and repeats the process.

while (booleanExpression)
{
    expressions;
}

DO…WHILE

The following command executes expressions. It then evaluates booleanExpression. If this evaluates to true, control returns to the top of the loop and repeats the process.

do
{
    expressions;
} while(booleanExpression);

FOR

The following command executes initCommand which may be an expression or a variable declaration. It then evaluates boolExpression. If this evaluates to true, then control passes to expressions1. If boolExpression is false, then control passes to the first statement after the closed brace of the for loop. Once expressions completes, control passes to the expression contained in loopExpression before returning to boolExpression to repeat the process. If initCommand declares a new variable, it goes out of scope as soon as control passes outside of the loop.

for (initCommand; boolExpression; loopExpression)
{
    expressions;
}

FOR (EACH)

The 2011 standard introduces a second form of for loop sometimes known as a "for each" because of its similarity to the foreach found in some other languages. In this form, the variable declared in declaration takes the value of the first member of list and executes the expressions block. When complete, the declared variable takes the second value of list and executes expressions again. This process is repeated for each value in list.

for (declaration: list)
{
    expressions;
}

SWITCH

The following command evaluates integerExpression and compares the result to each of the cases listed. If the value is found to equal one of the constant integral values, val1, val2, etc., control passes to the corresponding set of expressions and continues until control encounters a break. If expression does not equal any of the values, control passes to the expressionsN following default.

switch(integerExpression)
{
  case val1:
            expressions1;
            break;
  case val2:
            expressions2;
            break;
  [default:
            expressionsN;
  ]
}

BREAK, CONTINUE, GOTO

A continue passes control to the end of the closed brace of any of the looping controls. This causes the loop to continue with the next iteration. For example, the following loop processes prime numbers between 1 and 20:

for(int i = 0; i < 20; i++)
{
    // if the number is not prime...
    if (!isPrime(i))
    {
        // ...skip over to the next value of i
        continue;
    }
    // proceed on processing 
}

A break passes control to the first statement after the closed brace of any of the looping commands. This causes execution to exit the loop immediately. For example, the following reads characters until and end-of-file is encountered:

while(true)
{
    // read a line from input object
    input >> line;
    // if a failure or end-of-file occurs...
    if (cin.eof() || cin.fail())
    {
       // ...then exit the loop
       break;
    }
    // process the line
}

A goto label passes control to the label provided. The break example above could have been written as follows:

while(true)
{
    // read a line from input object
    input >> line;
    // if a failure or end-of-file occurs...
    if (cin.eof() || cin.fail())
    {
       // ...then exit the loop
       goto exitLabel;
    }
    // process the line
}
exitLabel:
    // control continues here
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

SERIES
C++ for Dummies Cheat Sheet

Advertisement

Inside Dummies.com