Break, Continue and Goto Statement in C++

The Break Statement in C++

The break and continue statement alter the flow of control. The break statement, When executed in a while, for, do/while or switch structure, causes an immediate exit from that structure. Program execution continues with the first statement after the structure. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch structure. When the if structure detects that x is 5, break executes. This terminates the for structure and the program continues with the cout after the for. The loop body executes fully only four times. Note that the control variable x in this program is defined outside the for structure header. This is because we intend to use the control variable both in the body of the loop and after the loop completes its execution

Break Statement in For loop can be written as

for(intitialization;condition;inc/dec)
{
-
-
-
break;
-
-
-
}

Break Statement in while loop can be written as

while(condition)
{
-
-
break;
-
-
-
}

Break Statement In do-While Loop Can Be Written As

do
{
-
-
break;
-
-
}while(condition);

1.Example of Break statement in C++

#include<iostream>
using std::cout;
using std::endl;
int main()  //main function declaration
{
int x;  //x declared here so it can be used after the loop 
// loop execution 10 times
for(x=1;x<=10;x++)
{
if(x==5)
break;  // break loop if x is 5
cout<<x<<"";  //display the value of x

}//end for loop
cout<<"\n Broke out of loop when x became"<<x<<endl;
return 0;  //indicate successful termination  

}// end function main

OUTPUT:

1 2 3 4
Broke out of loop when x became 5

The Continue statement in C++

The continue statement, when executed in a while, for or do/while structure skips the remaining statements in the body of that structure and proceeds with the next iteration of the loop. In a while and do/while structures, the loop-continuation test evaluates immediately after the continue statement executes. In the for structure, the increment expression executes, then the loop-continuation test evaluates. Earlier, we stated that the whole structure could be used in most cases to represent them for structure. The one exception occurs when the increment expression in the while structure follows the continue statement. In this case, the increment does not execute before the program test the repetition-continuation condition and the while does not execute in the same manner as for.uses the continue statement in a for the structure to skip the output statement in the structure and begin the next iteration of the loop

Continue statement in For loop can be written as

 for(intitialization;condition;inc/dec)
{
-
-
continue;
-
-
} 

Continue Statement In while Loop Can Be Written As

while(condition)
{
-
-
continue;
-
-
}

Continue Statement In do-while Loop Can Be Written As

do
{
-
-
continue;
-
-
}while(condition);

1.Example of Continue statement in c++

#inclued<iostream>
using std::cout;
using std::endl;
//function main begins program execution
int main()
{
//loop execution 10 times
for(int x=1;x<=10;x++)
{
//if x is 5, continue with next iteration of loop
if(x==5)
continue;   //skip remaining code in loop body
cout<<x<<" ";
}
cout<<"\n Used continue to skip printing the value 5"
<<endl;
return 0;    //indicate successful termination

}  //end function main

OUTPUT:

1 2 3 4 5 6 7 8 9 10
Used continue to skip printing the value 5

Goto Statement in C++

C++ also offers a goto statement .this allows you to jump to any given point marked by a label within a function.

1.Example Of A Goto Statement.

include 
 using namespace std; 
 int main() 
 { 
 ineligible: 
 cout<<"You are not eligible to vote!\n";  cout<<"Enter your age:\n";  int age;  cin>>age; 
 if (age < 18){ 
 goto ineligible; 
 } 
 else 
 { 
 cout<<"You are eligible to vote!"; 
 } 
 } 

OUTPUT:

 You are not eligible to vote!
 Enter your age:
 16
 You are not eligible to vote!
 Enter your age:
 7
 You are not eligible to vote!
 Enter your age:
 22
 You are eligible to vote! 

Difference between Break and Continue Statement

Break StatementContinue Statement
A break can appear in both switch and loop such as (for, while, do) statementA continue can appear only in the loop (for, while, do) statements
A break causes the switch or loop statements to terminate the moment it is executed. Loop or switch ends abruptly when the break is encounteredA continue doesn’t terminate the loop, it causes the loop to go to the next iteration. All iterations of the loop are executed even if continue is encountered
The break statement can be used in both switch and loop statements The continue statement can appear only in loops. You will get an error if this appears in switch statements
When a break statement is encountered, it terminates the block and gets the control out the switch or loop When a continue statement is encountered, it gets the control to the next iteration of the loop
Translate »