Loop Control / Branching Statements in C++

These control statements are used to change the normal sequence of execution of loop.

Following are the Loop control statements:

1. Break Statement
2. Continue Statement
3. Goto Statement

1. Break Statement

  • Break statement is used to terminate loop or switch statements.
  • It is used to exit a loop early, breaking out of the enclosing curly braces.
Syntax:
break;

Flow Diagram of Break Statement
flow diagram of break loop

Example : Demonstrating the Break statement's execution

#include <iostream>
using namespace std;
int main()
{
     int cnt = 0;
     do
     {
          cout<<"Value: "<<cnt<<endl;
          cnt++;
          if(cnt>5)
          {
               break;   //terminate the loop
          }
     }
     while(cnt<10);
     return 0;
}


Output:
Value: 0
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

2. Continue Statement

  • Continue statement skips the remaining code block.
  • This statement causes the loop to continue with the next iteration.
  • It is used to suspend the execution of current loop iteration and transfer control to the loop for the next iteration.
  • Syntax:
    continue;
  • In For Loop, continue statement causes the conditional test and increment/ decrement statement of the loop gets executed.
  • In While Loop, continue statement takes control to the condition statement.
  • In Do-While Loop, continue statement takes control to the condition statement specified in the while loop.
Flow Diagram of Continue Statement
flow diagram continue statement

Example : Demonstrating the execution of Continue statement

#include <iostream>
using namespace std;
int main()
{
     int cnt = 0;
     do
     {
          cnt++;
          if(cnt>5 && cnt<10)
              continue;
              cout<<"Value: "<<cnt<<endl;
     }
     while(cnt<11);
     return 0;
}


Output:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 10
Value: 11

3. Goto Statement

  • Goto statement transfers the current execution of program to some other part.
  • It provides an unconditional jump from goto to a labeled statement in the same function.
  • It makes difficult to trace the control flow of program and should be avoided in order to make smoother program.

  • Syntax:
    goto label;
    .
    .
    label: Statement;

    Label is an identifier that identifies a labeled statement, followed by a colon (:).

  • It is used to exit from deeply nested looping statements.
  • If we avoid the goto statement, it forces a number of additional tests to be performed.
Flow Diagram of Goto Statement
flow diagram goto statement

Example : Demonstrating the execution of Goto Statement

#include <iostream>
using namespace std;
int main ()
{
     int no = 0;
     label1:do
     {
         if( no == 4)
         {
             no = no + 1;    //Skip the iteration
             goto label1;
         }
         cout << "Value : " << no << endl;
         no = no + 1;
     }while( no < 5 );
     return 0;
}


Output:
Value : 0
Value : 1
Value : 2
Value : 3
Value : 5