Jump statements in c++: break, Continue and goto

Jump statements are used to interrupt the normal flow of a program. Jump statements transfers controls from one point to another point in the program due to some specified code while executing.

There are three jump statements in c++

  1. Break
  2. Continue
  3. Goto

Break  Statement 

Break statement is used to break the flow of loop. When break statement is found inside a loop the loop is immediately exited and program continues with other statement below the loop.

When the loops are nested, the break would only exit from the loop containing it.

Example:

for(int i=1;i<=10;i++)

{

      cout<<i;

      break;

}

Output:

1

Continue Statement

The continue statement breaks one iteration in a loop if a specified condition occurs and continue with the next iteration.

Example:

for(int i=0;i<=10;i++)

{

       if(i==5)

           continue;

           cout<<i;

}

output:

01234678910

Goto Statement

It is used to transfer control to some other part of a program.

Syntax:

label_1_name:

     statement;

label_2_name

     statement;

goto label_name;

Here,

Label is a identifier, when goto lbl_name is found the control is jumps to lbl_name and execute code below it.

Example:

  label :

cout<<"hello";

cout<<"Do you want to try again?";

cin>>i;

if(i=="y")

        goto label;


0 Comments:

Post a Comment

If you have any queries or suggestions, please let me know!