C++ Jump Statements

Advertisements

Jump Statements in C++

Jump statements are used to interrupt the normal flow of program.

Types of Jump Statements

  • Break
  • Continue
  • GoTo

Break Statement

The break statement is used inside loop or switch statement. When compiler finds the break statement inside a loop, compiler will abort the loop and continue to execute statements followed by loop.

Example of break statement


	#include<iostream.h>

       void main()
       {
              int a=1;

              while(a<=10)
               {
                   if(a==5)
                     break;

                    cout << "\nStatement " << a;
                   a++;
                }
                    cout << "\nEnd of Program.";
       }

   Output :

              Statement 1
              Statement 2
              Statement 3
              Statement 4
              End of Program.

Continue Statement

The continue statement is also used inside loop. When compiler finds the break statement inside a loop, compiler will skip all the followling statements in the loop and resume the loop.

Example of continue statement


	#include<iostream.h>

       void main()
       {
              int a=0;

              while(a<10)
               {

                   a++;

                   if(a==5)
                     continue;

                    cout << "\nStatement " << a;

                }
                    cout << "\nEnd of Program.";
       }

   Output :

              Statement 1
              Statement 2
              Statemnet 3
              Statement 4
              Statement 6
              Statement 7
              Statement 8
              Statement 9
              Statement 10
              End of Program.

Goto Statement

The goto statement is a jump statement which jumps from one point to another point within a function.

Syntax of goto statement


              goto label;
                    - - - - - - - - - -
                    - - - - - - - - - -
              label:
                    - - - - - - - - - -
                    - - - - - - - - - -
			

In the above syntax, label is an identifier. When, the control of program reaches to goto statement, the control of the program will jump to the label: and executes the code after it.

Example of goto statement


	#include<iostream.h>

       void main()
       {
                    cout << "\nStatement 1.";
                    cout << "\nStatement 2.";
                    cout << "\nStatement 3.";

                    goto last;

                    cout << "\nStatement 4.";
                    cout << "\nStatement 5.";

                    last:

                    cout << "\nEnd of Program.";
       }

   Output :

              Statement 1.
              Statement 2.
              Statement 3.
              End of Program.

Related topics

Advertisement