Skip to main content

Control Flow

break and continue

In this article we will learn about break and continue statements.

The break and continue statements are used inside a loop to alter its flow. The break statement terminates the loop immediately when it is encountered. And the continue statement allows us to skip statements inside the loop.

break

Imagine you are attending a boring lecture and you get a call from your friend for a treat at the canteen. What is coming to your mind?? For me, It would be “Lets take a break from the class and go to the canteen”. Break statements come to rescue for achieving this in C++.

The break statement terminates the loop immediately when it is encountered.

Example

#include <iostream>
using namespace std;
int main() {
 
    for (int i = 1; i < 6; ++i) {
        cout << i << endl;
        break;
    }
 
    cout << "This is outside the loop";
 
    return 0;
}

Output

1
This is outside the loop 

Here, the loop should iterate 5 times from i equal to 1 to 5. However, after printing 1 (the value of i), the break statement is encountered and the loop terminates.

In most cases, we'll want to terminate the loop in the middle only when a specific condition is met. That's why the break statement is almost always used inside an if statement. It’s more like you are done for a specific task and want a break from it.

Quiz

continue

Now, imagine you are reading a book and you come across a boring chapter and you want to skip that chapter and continue with the next one. "Continue" helps you in doing that in C++.

The  continue statement skips the code inside the loop for that current iteration. The loop will not terminate but continues on with the next iteration.

Example

Suppose a book has 5 chapters , 3rd one being the boring one which you want to skip.

#include <iostream>
using namespace std;
 
int main() {
  for (int i = 1; i <= 5; i++) {
        // condition to check boring chapter number
        if (i == 3) {
            continue; //Skip reading the 3rd chapter
        }
 
      cout << i <<":"<<"Chapter Read"<< endl;
  }
 
    return 0;
}

Output

1:Chapter Read
2:Chapter Read
4:Chapter Read
5:Chapter Read

Here, our loop iterates 5 times from i equal to 1 to 5.In the loop, we have a condition to check the boring chapter number inside the loop.

If i is 3 which is boring chapter number, continue is executed. Hence, the number won't be printed and the loop continues to the next iteration. If the chapter is interesting, continue won't be executed, so it will be printed. Only then does the loop go to the next iteration.

Quiz

Practice Problems

Problem 1

Create a program to calculate the sum of integers entered by the user until the user enters 0 or negative integer. Initialise a variable named total with value 0 at the beginning.

Note: The negative number shouldn't be added to the totalvariable.

Try it out yourself here!

Steps to follow

  • Initialize a variable named total with value 0 at the beginning.
  • Create a while loop with condition always true.
  • If the user enters 0 or a negative integer, use break to terminate the loop.
  • If the user enters a positive number, add it to the total variable.
  • Print the total outside of the loop.
Solution
#include <iostream>
using namespace std;

int main() {
    int num;
    int total = 0;
    // Continue looping until the user enters 0 or a negative integer
    while (true) {
            // Get an integer input from the user
            cout<<"Enter an integer (0 or negative to exit): "<<endl;
            cin>>num;

            // Check if the input is 0 or negative, and break the loop if true
            if (num <= 0) {
                break;
            }

            // Add the value to the total
            total += num;
    }
    // Print the total sum
    cout<<"Total sum: "<<total;
    return 0;
}

Problem 2

Create a program to print odd numbers between 1 and n, where n is a positive integer entered by the user.

Try it out yourself here!

Steps to follow

  • Take integer input from the user and store it in the variable n. We will assume the user will always enter a positive integer.
  • Use a for loop to iterate from i equal to 1 to n.
  • If i is even, use continue to skip the number from printing.
  • If i is odd, print i.
Solution
#include <iostream>
using namespace std;

int main() {
    int n;
    
    // Get an integer input from the user
    cout<<"Enter an integer (0 or negative to exit): "<<endl;
    cin>>n;
    
    for(int i=1;i<=n;i++){
        //if i is even, use continue to skip the number from printing
        if(i%2==0){
          continue;
        }
        //if i is odd, print the number
        else{
          cout<<i<<endl;
        }
    }
    
    return 0;
}

We are done with break and continue concepts. I hope now you are confident with these concepts. It is important to understand the fundamentals and have your tools ready for problem solving.

See you in the next article with more energy and excitement ✌️ till then Happy Learning !! 😀