Wednesday, 12 November 2014

Jump statements in C++ with examples

Jump statements in C++ with examples:

The break statement:

There are statements built in C++ programming to alter the normal flow of program.
The for, while, do-while ate used to perform the repetitive task till the expression is not valid.
There are the cases that we need to avoid some statements or we need to terminate the loop immediately after the some results as per our expectations. In C++ we have the “break and continue” statements.
The break; statement is also used to terminate switch statement.
The “break” statement is used to come out of the loop even the loop is not fulfilled. This break statement is also used to terminate the infinite loops.


How to use break statement in the while loop?
while (expression) {

  Statement - 1;
  Statement - 2;

  if (expression) {
    break;
  }
  Statement - N;
}

How to use break statement in the do-while loop?
do {

  Statement - 1;
  Statement - 2;
  if (expression) {
    break;
  }
  Statement - N;
} while (expression);

How to use break statement in the for loop?
for (initialization; expression; increment/decrement) {

  Statement - 1;
  Statement - 2;
  if (expression) {
    break;
  }
  Statement - N;
}

The example program that helps us to understand the break statement:

#include <iostream>
# include <stdio.h>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
   int iVal, iRes = 0;
    while (true) {     // test expression is always true
        cout<<"Enter a iVal: ";
        cin>>iVal;
        if (iVal != 0) {
            iRes += iVal;
        }
        else {
            break;    // terminating the loop if iVal equals to 0.0
        }
    }
    cout << "Result is = " << iRes;
    getchar ();
    return 0;
}

The output of the above program is

Enter a iVal: 10
Enter a iVal: 2
Enter a iVal: 2
Enter a iVal: 2
Enter a iVal: 3
Enter a iVal: 4
Enter a iVal: 5
Enter a iVal: 0
Result is = 28


The continue statement

The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration. For example, we are going to skip the number 10 in our countdown:

How to use break statement in the while loop?
while (expression) {
  Statement - 1;
  Statement - 2;
  if (expression) {
    continue;
  }
  Statement - N;
}

How to use break statement in the do-while loop?
do {

  Statement - 1;
  Statement - 2;
  if (expression) {
    continue;
  }
  Statement - N;
} while (expression);

How to use break statement in the for loop?
for (initialization; expression; increment/decrement) {
  Statement - 1;
  Statement - 2;
  if (expression) {
    continue;
  }
  Statement - N;
}
Example program for the “continue” statement:
#include <iostream>
#include <stdio.h>
using namespace std;

int _tmain (int argc, _TCHAR* argv[])
{
  for (int iInd = 100; iInd > 0; iInd--)
  {
    if (iInd == 10)
      continue;
    cout << iInd << ", ";
  }
   cout << "out of the loop!\n";
   gethcar ();
   return 0;
} 

The output of the above program is 
100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81,

 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61,

 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41,

 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21,

 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, out of the lo

op!


The goto statement
goto allows to make an absolute jump to another point in the program. You should use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations.
The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made of a valid identifier followed by a colon (:).

Syntax of the goto statement:
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
Example program for goto statement:
#include <iostream>
#include <stdio.h>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
  float iVal, iAVG, iResu = 0.0;
    int i, n;
    cout<<"Maximum number of inputs: ";
    cin>>n;
    for(i=1; i <= n; ++i) {
        cout<<"Enter n"<<i<<": ";
        cin>>iVal;
        if(iVal < 0.0) {
            goto jump;  
        }
        iResu += iVal;
    }
jump:
    iAVG=iResu / (i-1);
    cout<<"\naverage value = "<<iAVG;
 getchar ();
  return 0;
}

The output of the above program is 
Maximum number of inputs: 3
Enter n1: 2
Enter n2: 3
Enter n3: 2
average value = 2.33333
NOTE: It is the good idea that not to use goto statements in C++, it not so good of using goto statements.

What is the reason to Avoid goto Statement?

The goto statement gives power to jump to any part of program but makes the logic of the program difficult and jumbled.  We can replace the goto statement using the break and continue statements.

The exit () function:

The exit function is available in cstlib.
To use the exit function we need to include
 #include <cstlib.h>

The purpose of exit is to terminate the current program with a specific exit code.
The syntax of the exit function is shown below
 void exit (int code);

The exit code is used by some operating systems and may be used by calling programs.

No comments: