Bifurcation of Control

The break instruction.

Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. For example, we are going to stop the count down before it naturally finishes (an engine failure maybe):

// break loop example Output

#include <iostream.h> 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!

int main ()

{

int n;

for (n=10; n>0; n--) {

cout << n << ", ";

if (n==3)

{

cout << "countdown aborted!";

break;

}

}

return 0;

}

The continue instruction.

The continue instruction causes the program to skip the rest of the loop in the present iteration as if the end of the statement block would have been reached, causing it to jump to the following iteration. For example, we are going to skip the number 5 in our countdown:

// break loop example Output

#include <iostream.h> 10, 9, 8, 7, 6, 4, 3, 2, 1, SHOOT!

int main ()

{

for (int n=10; n>0; n--) {

if (n==5) continue;

cout << n << ", ";

}

cout << "SHOOT!";

return 0;

}

The goto instruction.

It allows making an absolute jump to another point in the program. You should use this feature carefully since its execution ignores any type of nesting limitation. The destination point is identified by a label, which is then used as an argument for the goto instruction. A label is made of a valid identifier followed by a colon (:). This instruction does not have a concrete utility in structured or object oriented programming aside from those that low-level programming fans may find for it. For example, here is our countdown loop using goto:

// goto loop example Output

#include <iostream.h> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, SHOOT!

int main ()

{

int n=10;

loop:

cout << n << ", ";

n--;

if (n>0) goto loop;

cout << "SHOOT!";

return 0;

}

The Return StatementA function returns to its calling function by the return statment. A return statement ithout an expression can be used only in functions that do not return a value, that is, a function with the return type void. A return statement with an expression can be used only in functions returning a value: the value of the expression is returned to the calling function.

The Exit Function

The function exit() is defiend in the C++ standard library <stdlib.h>. the purpose of exit() is to terminate the program in course with a epecific exit code. Its prototype is :

void exit(int exit code);

The exit code is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means an error happened.

Nested Loops

A loop may contain another loop in its body. this form of a loop is called nested loop. the inner and outer loops may not be of the same construct. An outer loop and inner loop cannot have same control variable. The inner loop must be completely nested inside the body of the outer loop. The inner loop must terminate before the outer loop. Outer loop opens first but closes last.

HOME LEARN C++ PREVIOUS NEXT