Examples of loop break vs continue

Estimated reading: 3 minutes 170 Views

break and continue are two flow control statements in C++ that can be used to alter the normal flow of a loop.

break terminates the loop immediately and unconditionally.

continue skips the rest of the current iteration of the loop and causes the loop to continue with the next iteration.

Here is an example of how to use continue in C++:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 0; i < 10; i++)
    {
        if (i == 5)
            continue;
        cout << "i = " << i << endl;
    }
    
    return 0;
}

This code is a for loop that prints the values of the variable i to the console, from 0 to 9, inclusive, except for the number 5. The if statement inside the loop checks if i is equal to 5. If it is, the continue statement skips the rest of the current iteration of the loop and causes the loop to continue with the next iteration.

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares a variable i and initializes it to 0.
  2. The program starts a for loop that iterates 10 times.
  3. Inside the for loop, the program checks if i is equal to 5.
  4. If i is equal to 5, the program skips the rest of the current iteration of the loop using the continue statement.
  5. Otherwise, the program prints the value of i to the console.
  6. The program then increments i by 1.
  7. The for loop repeats steps 3-6 until it has iterated 10 times.

Here is the output of this code:

i = 0
i = 1
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9

Here is an example of how to use break in C++:

#include <iostream>

using namespace std;

int main()
{
    for (size_t i = 0; i < 10; i++)
    {
        if (i == 4)
            break;
        cout << "i = " << i << endl;
    }
    
    return 0;
}

This code is a for loop that prints the values of the variable i to the console, from 0 to 3, inclusive. The if statement inside the loop checks if i is equal to 4. If it is, the break statement terminates the loop immediately and unconditionally.

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares a variable i and initializes it to 0.
  2. The program starts a for loop that iterates 10 times.
  3. Inside the for loop, the program checks if i is equal to 4.
  4. If i is equal to 4, the break statement terminates the loop immediately and unconditionally.
  5. Otherwise, the program prints the value of i to the console.
  6. The program then increments i by 1.
  7. The for loop repeats steps 3-6 until it has iterated 10 times or until the loop is terminated by the break statement.

In this case, the loop will be terminated by the break statement when i is equal to 4. Therefore, the output of the code will be:

i = 0
i = 1
i = 2
i = 3

Which one to use?

Which one to use depends on what you want to achieve. If you want to terminate the loop immediately, use break. If you want to skip the rest of the current iteration of the loop and continue with the next iteration, use continue.

Here are some general tips for using break and continue:

  • Use break sparingly. Terminating a loop early can make your code more difficult to read and understand.
  • Use continue to avoid writing duplicate code. If you find yourself writing the same code inside a loop multiple times, consider using continue.
  • Use break and continue to make your code more efficient. For example, you can use break to terminate a loop early if you have already found what you are looking for.
Share this

Examples of loop break vs continue

Or copy link

CONTENTS
English