C++ Break and Continue C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ Break and Continue
C++ Break
You have already seen the break
statement used in earlier topics of this tutorial. It was used to "jump out" of a switch
statement. The break
statement can also be used to jump out of a loop.
Example
#include <iostream>
using namespace std;int main() {
for (int i = 0; i < 10; i++) {
if (i == 3) {
break;
}
cout << i << "\n";
}
return 0;
}
Output:
0
1
2
C++ Continue
The continue
statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
This example skips the value of 5:
Example
#include <iostream>
using namespace std;int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
cout << i << "\n";
}
return 0;
}
Output:
0
1
2
3
4
6
7
8
9