C++ Course Listing

 

Program Flow Control

The flow of the program may be altered with loops and branch statements.

Contents

Loops

For Loops

While Loops

Do While Loops

Continue

Break

Branch Statements

If

Switch & Case

Goto

top of page

Loops


There are three types of loops: for, while and do. In addition there are the continue and break commands that are used to modify the behaviour of loops.


For Loops


The for loop has the following syntax:

for (initialisation; end-condition; incrimination)
{
//statement block to be executed within the loop.
}

The initialisation sets the initial value of the variable being used to keep track of the number of times the loop has been iterated. The end-condition determines when the loop is ended. The incrimination is performed every time the loop is completed.
e.g. the following for loop will print the word Hello to the screen ten times.

for (i = 0; i<10; i++)
{
printf (“\nHello”);
}

top of page


While Loops


The while loop tests the condition for the loop at the beginning of the loop. This has the effect that is the condition is not true before the loop has been entered, the loop will never be entered. The while loop has the following syntax:

while (condition)
{
//statement block to be executed within the loop.
}

e.g. the following while loop will read in 50 characters and place them in the character array s.

char s[51];
int j;

j=0;

while (j<50)
{
s[j] = stream.get();
}

top of page


Do While Loops


The do while loop is similar to the while loop but the condition is placed at the end of the loop. This has the affect that the loop is always entered and iterated at least once regardless of whether the condition is true or not when the loop is entered. The syntax of the do while loop is:
do
{
//statement block to be executed within the loop.

}while (condition)

e.g. using the same example as for the while loop (above):

do
{
s[j] = stream.get();
}while (j<50)

top of page


Continue


The continue command causes the current iteration of the loop to stop and for the condition of the loop to be immediately accessed.


Break


The break command causes the loop currently being executed to be stopped, regardless of the evaluation of the condition.

top of page

Branch Statements


There are x branch statements: if, switch & case, goto. The most important of these are the if and the switch & case statements. The goto statement is considered poor programming technique in C and C++ by some purists.


If


The if statement is used to evaluate a condition and alter the flow of code execution depending upon the outcome of the evaluation. If can be used in combination with else, for multiple assessments and branch points. If has the following syntax:

if (condition)
{
//statement block to be executed within the loop.
}

A single else statements may be tacked on the end if required.

e.g. if x is less than zero than an error message is printed to the screen:

if (x<0)
{
printf (“Error!”);
}

top of page


Switch & Case


The switch statement is much like an if statement with multiple else statements. A default may be used at the end as an optional catchall. Switch has the following syntax:

switch (expression)
{
case 1: statement;
break;
case 2: statement;
break;

default: statement;
}

Each case statement ends with a break, which results in the program execution jumping straight to the end of the switch statement.

e.g. the following switch statement assesses the value of the char s and calls a different function depending on the value of s:

switch (s)
{
case ‘a’: addition();
break;
case ‘b’: subtraction();
break;
case ‘c’: multiply();
break;
case ‘d’: divide();
break;
case ‘q’: exit(0); //quit
break;
default: printf(“\nOnly select a, b, c, d or q.”);
}

top of page


Goto


The goto command causes the execution of the program to jump to a predefined label. The use of goto is considered bad programming practice by some C purists, but it can have its uses. The syntax for the goto statement is:

goto mylabel;

mylabel:

by Matthew Martin

top of page