C++ break, continue, and goto statements

In this post, you will learn about the following jump statements, which are used to transfer program control in C++.

One other jump statement is "return." The return statement is discussed in a separate post, whereas the other three statements are going to be covered in this post.

The keywords "return" and "goto" can be used in any part of the program, whereas the keywords "break" and "continue" are restricted to the most contained portions of the program, such as loops. In addition to the four jump statements that are listed above, we also have a method that is denoted by the name "exit()," and it is responsible for halting any further execution of the program.

C++ break statement

The break statement enables a program to skip over part of the code. A break statement terminates the smallest enclosing for, while, do-while, or switch statement. Execution resumes at the statement immediately following the body of the terminated statement. For example,

#include<iostream>
using namespace std;
int main()
{
   int a, b, c, i;
   for(i=0; i<10; i++)
   {
      cout<<"Enter two numbers: ";
      cin>>a>>b;
      if(b==0)
         break;
      else
         c = a/b;
      cout<<a<<"/"<<b<<" = "<<c<<"\n\n";
   }

   return 0;
}

The following snapshot shows the initial output produced by the above C++ example program.

c++ break statement example

Now the program continues receiving two numbers from the user to print "numerator/denominator = quotient" on the output for 10 times, or until the user enters "0" as the denominator in any case. The following snapshot shows the sample run with some user inputs, including one zero as the denominator on the third count.

c++ break statement program

That is, when we enter "0" as the second value in any case of the allowed 10, the "0" will be initialized to "b," the condition "b==0" evaluates to true, and the "break" statement will be executed, which terminates the further execution of the loop.

C++ continue statement

Unlike the "break" statement, the "continue" statement is used when we need to skip the remaining statements of the loop's body and continue for the next iteration of the loop. For example:

#include<iostream>
using namespace std;
int main()
{
   int a, b, c, i;
   for(i=0; i<5; i++)
   {
      cout<<"Enter two numbers: ";
      cin>>a>>b;
      if(b==0)
         continue;
      else
         c = a/b;
      cout<<a<<"/"<<b<<" = "<<c<<"\n\n";
   }

   return 0;
}

The snapshot given below shows a sample run of the above program with some user inputs:

c++ continue statement example

As you can see, I supplied 0 as the second number (denominator) for the second, third, and fourth counts. Therefore, in that case, since the condition "b == 0" evaluates to true, the "continue" statement must be executed, which further skips the "cout" statement, which is available at the bottom (after the continue) of the loop. However, the loop continued its execution.

difference between the break and continue statements in C++

The break statement is used to terminate the further execution, whereas the continue statement is used to continue for the next iteration by skipping the remaining statements that are available after the "continue" statement and inside the block. For example:

#include<iostream>
using namespace std;
int main()
{
   cout<<"----using break statement----\n";
   for(int x = 1; x<=10; x++)
   {
      if(x==5)
         break;
      cout<<"x = "<<x<<endl;
   }

   cout<<"\n\n";

   cout<<"----using continue statement----\n";
   for(int x = 1; x<=10; x++)
   {
      if(x==5)
         continue;
      cout<<"x = "<<x<<endl;
   }

   return 0;
}

The output should be exactly:

c++ break vs. continue example

As you can see from the above output, when using "break,", the further loop execution terminates. Whereas in the case of the "continue" statement, only the remaining statement skips, and the loop continues its execution.

Do not confuse the "break" (exit the block) and "continue" (exit the remaining statement(s)).

C++ goto statement

The goto statement can transfer program control anywhere in the program. The target destination of a goto statement is marked by a LABEL. The target LABEL and goto must appear in the same function.

The general form of the "goto" statement is as follows:

LABEL:
   .
   .
   .
goto LABEL;

where LABEL is a user-supplied identifier and can appear either before or after goto. For example:

#include<iostream>
using namespace std;
int main()
{
   int a=10, b=20;

   CODESCRACKER:
      if(a%2 == 0)
         cout<<a<<endl;
      a++;
      if(a<=b)
         goto CODESCRACKER;

   return 0;
}

Following is the output of the above C++ example program on the "goto" statement.

10
12
14
16
18
20

That is, the above program prints all the even numbers between 10 and 20, including both the numbers.

In the above program, when the condition "a<=b" evaluates to false, then the following statement is true:

goto CODESCRACKER;

will not be executed, which means that the block of code available inside the "CODESCRACKER" label will stop its execution.

C++ exit() function

The C++ "exit()" method is used when we need to stop the execution of the program. For example:

#include<iostream>
using namespace std;
int main()
{
   cout<<"codescracker.com";
   exit(0);
   cout<<"codescracker.com";

   return 0;
}

This program will produce the following output:

codescracker.com

That is, the second "codescracker.com" will not be printed because the "exit(0)" method terminates the further execution of the program. Therefore, anything written after the "exit()" method will not be executed.

There is no return value for the exit() function. Its argument, 0 in the preceding program, is returned to the operating system. In general, a value of 0 indicates a successful termination, while any other number indicates an error.

C++ Quiz


« Previous Tutorial Next Tutorial »


Follow/Like Us on Facebook


Subscribe Us on YouTube