Jumping statements in C++ | break statement in C++ | continue statement in C++ | goto statement in C | exit in C++

Jumping statements in C++ | break statement in C++ | continue statement in C++ | goto statement in C++ | exit in C++

There are four types of jumping statements in C++.

 1. break statement in C++

break is a keyword of C++. It is used with  switch and looping statements. It is used to transfer the control of program out of  switch or looping statement.

Program of break statement in C++

#include<iostream> 
using namespace std;
int main()
{
 for(int i=1;i<=6;i++)
 {
   if(i==3)
     break;
   cout<<endl<<i;
}
 return 0;
}

Output

1
2

for statement starts with 1 and goes upto 5. When value of i becomes 3, break would execute and control would leave for statement hence nothing after 2 is displayed.

 2. continue statement in C++

continue statement in C++ is used with looping statements. It  skips all statements after it and proceeds with next iteration of a looping statement.

Program  of continue statement in C++

#include<iostream> 
using namespace std;
int main()
{
 for(int i=1;i<=6;i++)
 {
  if(i==3)
    continue;
  cout<<endl<<i;
}
 return 0;
}

Output

1
2
4
5

for statement starts with 1 and should go upto 5. When value of i becomes 3, continue statement would execute and control would go to the next iteration of for statement i.e. statements after continue would be skipped and value of i would become 4 and so on.

3. goto statement in C++

goto statement in  C++  is  used to transfer the program control from one part to another part of same program. 

Syntax of goto statement is:

goto Label;

goto is a keyword.

Label is any valid identifier of C++. It is defined by the programmer depending upon his own requirement. This label must appear somewhere  else in the same program. The label defined must be ended with a colon.

goto statement in C++ can be used in two ways:

a. Forward goto

The label appears after goto statement.

goto Label;
:
:
Label:

Program to use forward goto statement in C++

#include<iostream> 
using namespace std;
int main()
{
 cout<<"\nwelcome";
 goto stop;        //Line 6
 cout<<"\nHello"; //Line 7
 stop:            //Line 8
 cout<<"\nBye"; 
return 0;
}

Output

Welcome
Bye

Description of above program

In the above program, stop is the label defined by the programmer in Line 8. After displaying welcome the control would transfer after the label stop: i.e. Line 7 is skipped and control transfers to line 8.

b. Backward goto

In backward goto statement, the label exists before the goto statement.

Label:
:
:
goto Label;

Program to demonstrate the use of backward goto statement in C++

#include<iostream> 
using namespace std;
int main()
{
 int counter=1;
 start:                              /*Line 1*/
  cout<<endl<<counter;
 counter++;
 if(counter<=5)
   goto start;                         /*Line 2*/
 return 0;
}

Output

1
2
3
4
5

In the above program, start is the label defined by the programmer in Line 1. When the program is executed for the first time, value of counter=1 id displayed. After displaying value of counter, counter is incremented by 1. then condition counter<=5 is checked. If it is true program control transfers to line 1 again. In this way as long as condition is true, value of counter is displayed.

4. exit() function in C++

Whenever exit() function is found in a program, the program terminates and no statement after it would be executed.

Program to demonstrate the use of exit() function in  C.

#include<iostream> 
using namespace std;
int main()
{
  int n;
  cout<<”\nEnter an integer value=”;
  cin>>n;
  if(n==0)
  {
    cout<<”\nProgram terminated..”;
    exit(0);
  }
  else
    cout<<"\nWelcome";
 return 0; 
}

Output

Enter an integer value=0
Program terminated..

In the above program, If value of a is zero , program would terminated due to exit(0).

Spread the love
Lesson tags: break statement in c, continue statement in c, exit in C++, goto statement in c, jumping statements in C++
Back to: C++ Programming Notes
Spread the love