break | goto statement

A breaking statement in a program is used to terminate another control statement or execute or skip a particular statement.

The break statement of c ++ is divided into three parts which are as follows-

To avoid page complexity,we will discuss here only break-statement and goto statement

 

break statement

This statement is used to break or skip a set of statement or other control statement.

here is a syntex to use break statement

SYNTEX:-

{
  …………….;
  break;
}
#include<iostream.h>
#include<conio.h>
void main()
{
   clrscr();
   int x;       // variable declaration
   cout<<"Enter number smaller than 9: ";  
   cin>>x;
   for(int i=0; i<9;i++)
  {
      cout<<i<<" ";
      if(i==x)        // expression
          break;
  }
  cout<<"\nThis is next statement"; 
  getch();
}
OUTPUT:-
Enter number smaller than 9: 5
0 1 2 3 4 5
This is next statement

 

goto statement

This statement is used to control control in the program because it jumps from one part of the program to another part, hence it is also called jumping statement, which is in the other part of the definition program. its behave like a function.

we can use goto statement in two ways,

goto-statement

Here in the program, we use 1st,

#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int x;
    cout<<"Enter number smaller than 9: ";
    cin>>x;
    for(int i=0; i<9; i++)
    {
       if(i==x) 
          goto lable;       
       cout<<i;
    }
    lable:
   {
       cout<<"\nnumber is equal to "<<x;
   }
   getch();
}
OUTPUT:-
Enter number smaller than 9: 5
0 1 2 3 4 
number is equal to 5

let’s see other example where we use if statement and goto statement together to create a loop

Create a loop using goto and if statement

Three variables have been declared in the program, in which two variables num1 and num2 have been asked to input from the user.

cin>>num1>>num2;

While the third variable sum shows the total of both of these variables and the result is printed.

cout<<num1+num2;
cout<<"total: "<<sum;

Here is the example,below

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
  clrscr();
  int num1,num2,sum;
  start:
  cout<<"\nEnter two number: ";
  cin>>num1>>num2;

 if(num1== 0 || num2==0) // if input value 0 then program will be exit
   exit(0);

  sum = num1+num2;
  cout<<"Total: "<<sum;
  goto start;
}
OUTPUT:-
Enter two number: 3 4 
Total: 7
Enter two number: 6 3
Total : 9
Enter two number: 0 4

In the if-statement, if the condition is false, execution will go to the next statement, which will print the result.

After printing the result, the goto statement will also be executed, which executes the program again and the program will execute until the condition is true in the if-statement.

In other words, the program will remain in execution until either of the variable num1 and num2 has a value of 0.

In this way, this program will behave like a loop.

thing to know

goto statement used in a small program while in a large program we use function. but doesn’t means goto statement is not much useful than function. it is good programming practice to work with goto statement.


previous-do-while loop

next-continue and exit Statement