Break, Continue, and go-to statements in C++

Shailendra Chauhan  11 min read
21 Jul 2023
Beginner
1.17K Views

Introduction

As a programmer, understanding control structures like break, continue, and go-to statements are essential for writing efficient code in C++. If you’re just getting started with programming or brushing up on your C++ skills, then you can check our C++ certification course and this blog post will also help you! I’ll be discussing the basics of these three essential control systems and how they can affect the outcome of your code. No matter what level of experience you have with coding and C++ in particular – this post could give you some great insights that will help make your next project successful. So let's jump right into it!

What is the Break statement in C++?

The Break statement in C++ is a way of telling the compiler to terminate a loop. Break statements can occur anywhere inside the loop, allowing the programmer to control how much of the loop they want to execute before breaking out of it. Break statements are normally used in order to break a loop that has become unproductive or inefficient due to some kind of unusual condition occurring in between each iteration. Break statements are therefore an essential part of programming with C++, allowing for dynamic decisions within code and improving overall efficiency.
Syntax of Break statement in C++
jump-statement; 
break;
Example of Break statement in C++
#include <iostream> 
using class std; 
int main()
{ 
      for (int i = 1; i <= 10; i++) 
          { 
              if (i == 5) 
              { 
                  break; 
              } 
        cout<<i<<"\n"; 
          } 
} 
Output of Break statement in C++
1
2
3
4

Break statement with an inner loop in C++

Break statements with an inner loop in C++ can be extremely useful for making program execution more efficient. Break statements allow a programmer to skip certain iterations in a loop, ensuring that the loop does not execute irrelevant code or continue to run when it is no longer necessary. Inner loops are crucial for completing complex tasks and analyzing data, so using break statements to reduce their impacts can greatly improve program efficiency. Break statements with an inner loop should be used judiciously, as they can cause bugs if not implemented properly within a loop. Fortunately, the syntax of break statements with an inner loop in C++ is easy to understand and allows users to define exactly when and how the condition should be broken.
Example of Break statement with an inner loop in C++
#include <iostream> 
using class std; 
int main() 
{ 
  for(int i=1;i<=3;i++)
    { 
      for(int j=1;j<=3;j++)
        { 
          if(i==2&&j==2)
            { 
              break; 
            } 
          cout<<i<<" "<<j<<"\n"; 
        } 
    } 
} 
Output of Break statement with an inner loop in C++
1 1
1 2
1 3
2 1
3 1
3 2
3 3

What is a continue statement in C++

A continue statement in C++ is a type of control flow statement used to continue the program flow without executing the rest of the loop body after the continue statement. Instead, control immediately passes onto the next iteration of the loop through which it was invoked. continue statements are particularly useful when certain loops need to skip specific segments, like continue sending emails until there are no more email messages in a list. As such, C++ continue statements are an incredibly useful tool for streamlining and optimizing code performance.
Syntax of continue statement in C++
jump-statement; 
continue;
Example of continue statement in C++
#include <iostream> 
using class std; 
int main() 
{ 
  for(int i=1;i<=10;i++)
    { 
      if(i==5)
        { 
          continue; 
        } 
        cout<<i<<"\n"; 
    } 
} 
Output of continue statement in C++
1
2
3
4
6
7
8
9
10

Continue statement with an inner loop in C++

Continue statements are a useful tool within the C++ programming language and can be very helpful when writing an inner loop. Continue statements allow for a line of code within an expression that acts as a jump command for the loop: when used, the Continue statement will cause program execution to proceed directly to the next iteration of the inner loop, skipping any remaining instructions between the Continue line and next iterative point. This is especially convenient when dealing with nested loops that contain various checks and operations. Continue statements offer developers a flexible way to fine-tune their algorithms, making them run more efficiently by allowing sections of code inside an iterator to be jumped over as needed.
Example of Continue statement with an inner loop in C++
#include <iostream> 
using class std; 
int main() 
{ 
 for(int i=1;i<=3;i++)
 { 
  for(int j=1;j<=3;j++)
    { 
      if(i==2&&j==2)
        { 
          continue; 
        } 
       cout<<i<<" "<<j<<"\n"; 
    } 
  } 
} 
Output of Continue statement with an inner loop in C++
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

What are the Go-to statements in C++

Go-to statements are an important tool in the programmer's arsenal, and this is especially true for C++. Go-to statements in C++ allow the user to jump from one place in the code to another, making it easy to write programs with complex logic. Go-to statements save time by removing the need to repeat chunks of code. Additionally, they can help reduce complexity and make programs easier to read. Go-to statements are also used when dealing with errors or exceptions that need special handling. All of these features make Go-to statements a great tool for any programmer looking to write efficient, robust programs using C++.
Example
#include <iostream> 
using class 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:
14
You are not eligible to vote!

Enter your age:
8
You are not eligible to vote!

Enter your age:
19
You are eligible to vote!

Summary

In this blog post, we looked at break, continue, and go-to statements in C++. These are all important tools to understand when programming in C++. With these tools, you can control the flow of your program and make it do what you want it to do. If you need help understanding any of these concepts, then enroll in our C++ Training or if you need help with your C++ programming homework, be sure to reach out to us. We're here to help you every step of the way!
Share
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this