Difference between continue and break statements in C++ - GeeksforGeeks

14 Oct, 2018
Break and continue are same type of statements which is specifically used to alter the normal flow of a program still they have some difference between them.
break statement: the break statement terminates the smallest enclosing loop (i. e., while, do-while, for or switch statement)
continue statement: the continue statement skips the rest of the loop statement and causes the next iteration of the loop to take place.
an example to understand the difference between break and continue statement
// CPP program to demonstrate difference between// continue and break#include <iostream>using namespace std;main(){    int i;    cout << "The loop with break produces output as: \n";      for (i = 1; i <= 5; i++) {          // Program comes out of loop when        // i becomes multiple of 3.        if ((i % 3) == 0)            break;        else            cout << i << " ";     }      cout << "\nThe loop with continue produces output as: \n";    for (i = 1; i <= 5; i++) {          // The loop prints all values except        // those that are multiple of 3.         if ((i % 3) == 0)            continue;         cout << i << " ";     }}
The loop with break produces output as: 
1 2 
The loop with continue produces output as: 
1 2 4 5

Description of the program:
In first for loop, here we use the break statement.Now when the loop iterates for the first time, the value of i=1, the if statement evaluates to be false, so the else condition/ statement is executed.Again loop iterates now the value of i= 2, else statement is implemented as if statement evaluates to be false.Loop iterates again now i=3; if the condition evaluates to be true and the loop breaks.
In first for loop, here we use the break statement.
Now when the loop iterates for the first time, the value of i=1, the if statement evaluates to be false, so the else condition/ statement is executed.
Again loop iterates now the value of i= 2, else statement is implemented as if statement evaluates to be false.
Loop iterates again now i=3; if the condition evaluates to be true and the loop breaks.
In second for loop, here we use the continue statement.Now when the loop iterate for the first time then the value of i=1, the if statement evaluates to be false, so the else condition/ statement 2 is implemented.Again loop iterates now the value of i= 2, else statement is implemented as if statement evaluates to be false.Loop iterates again now i=3; if the condition evaluates to be true, here the code stop in between and start new iterate until the end condition met.
In second for loop, here we use the continue statement.
Now when the loop iterate for the first time then the value of i=1, the if statement evaluates to be false, so the else condition/ statement 2 is implemented.
Again loop iterates now the value of i= 2, else statement is implemented as if statement evaluates to be false.
Loop iterates again now i=3; if the condition evaluates to be true, here the code stop in between and start new iterate until the end condition met.
CPP-Control-Flow
Technical Scripter 2018
C++
C++ Programs
Difference Between
School Programming
Technical Scripter
CPP-Control-Flow
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Operator Overloading in C++
Iterators in C++ STL
Friend class and function in C++
Polymorphism in C++
Sorting a vector in C++
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
CSV file management using C++
Program to print ASCII Value of a character