C++ Flow Control Jump statements : break, continue, goto, exit.


Example

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

The syntax is

break;

Example: we often use break in switch cases,ie once a case i switch is satisfied then the code block of that condition is executed .

switch(conditon){
case 1: block1;
case 2: block2;
case 3: block3;
default: blockdefault;
} 

in this case if case 1 is satisfied then block 1 is executed , what we really want is only the block1 to be processed but instead once the block1 is processed remaining blocks,block2,block3 and blockdefault are also processed even though only case 1 was satified.To avoid this we use break at the end of each block like :

switch(condition){
case 1: block1;
        break;
case 2: block2;
        break;
case 3: block3;
        break;
default: blockdefault;
        break;
}

so only one block is processed and the control moves out of the switch loop.

break can also be used in other conditional and non conditional loops like if,while,for etc;

example:

if(condition1){
   ....
   if(condition2){
    .......
    break;
    }
 ...
}

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.

The syntax is

continue;

Example consider the following :

for(int i=0;i<10;i++){
if(i%2==0)
continue;
cout<<"\n @"<<i;
}

which produces the output:

 @1
 @3
 @5
 @7
 @9

i this code whenever the condition i%2==0 is satisfied continue is processed,this causes the compiler to skip all the remaining code( printing @ and i) and increment/decrement statement of the loop gets executed.

in for loop

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 (:)

The syntax is

goto label;
..
.
label: statement;

Note: Use of goto statement is highly discouraged because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify.

types of goto

Example :

int num = 1;
STEP:
do{
 
    if( num%2==0 )
    {
        num = num + 1;
        goto STEP;
     }

   cout << "value of num : " << num << endl;
   num = num + 1;
 }while( num < 10 );

output :

value of num : 1
value of num : 3
value of num : 5
value of num : 7
value of num : 9

whenever the condition num%2==0 is satisfied the goto sends the execution control to the beginning of the do-while loop.

The exit function:

exit is a function defined in cstdlib. The purpose of exit is to terminate the running program with an specific exit code. Its prototype is:

void exit (int exit code);

cstdlib defines the standard exit codes EXIT_SUCCESS and EXIT_FAILURE.