C++ Break and Continue
Control loop execution flow with break and continue statements
π What are Break and Continue?
Break and continue are control statements that modify loop behavior. Break exits the loop completely, while continue skips the current iteration and moves to the next one.
// Simple break example
for(int i = 1; i <= 5; i++) {
if(i == 3) break;
cout << i << " ";
}
// Output: 1 2
Output:
1 2
Key Concepts
Break Statement
Exits the loop immediately
if(condition) break;
Continue Statement
Skips current iteration
if(condition) continue;
Loop Control
Works with for, while, do-while
while(true) {
if(done) break;
}
Nested Loops
Affects only innermost loop
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(j==1) break; // Only inner
}
}
πΉ Break Statement Examples
The break statement immediately exits a loop or switch, terminating further iterations.
Itβs used to stop processing once a condition is met, like finding the first even number (2) or locating a target
value (30 at index 2) in an array. This prevents unnecessary cycles, optimizing performance. Break is crucial in
search algorithms, error handling, and menu-driven programs where early exit is required upon achieving a goal or
encountering a terminal condition.
#include <iostream>
using namespace std;
int main() {
// Finding first even number
for(int i = 1; i <= 10; i++) {
if(i % 2 == 0) {
cout << "First even number: " << i << endl;
break; // Exit loop when found
}
}
// Search in array
int arr[] = {10, 20, 30, 40, 50};
int target = 30;
for(int i = 0; i < 5; i++) {
if(arr[i] == target) {
cout << "Found " << target << " at index " << i << endl;
break;
}
}
return 0;
}
Output:
First even number: 2
Found 30 at index 2
πΉ Continue Statement Examples
The continue statement in C++ skips the current iteration of a loop and proceeds to the next
cycle. This is particularly useful when you want to bypass specific cases without terminating the loop
entirely. For instance, when printing odd numbers from 1 to 10, continue can skip even numbers,
outputting only 1, 3, 5, 7, and 9. Similarly, filtering positive numbers from a mixed list ensures only values like
5, 8, and 12 are processed, enhancing loop efficiency and control flow in your programs.
#include <iostream>
using namespace std;
int main() {
// Print only odd numbers
cout << "Odd numbers from 1 to 10: ";
for(int i = 1; i <= 10; i++) {
if(i % 2 == 0) {
continue; // Skip even numbers
}
cout << i << " ";
}
cout << endl;
// Skip negative numbers
int numbers[] = {-2, 5, -1, 8, -3, 12};
cout << "Positive numbers: ";
for(int i = 0; i < 6; i++) {
if(numbers[i] < 0) {
continue; // Skip negative
}
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Output:
Odd numbers from 1 to 10: 1 3 5 7 9
Positive numbers: 5 8 12
πΉ Practical Applications
In real-world programming, break and continue statements optimize performance and
control flow. break is ideal for exiting loops early, such as stopping a search once a
target is found, reducing unnecessary iterations. continue skips irrelevant data, like ignoring invalid
user inputs or non-critical events within a loop. These constructs are essential in game development for exiting
menus, in data processing for filtering datasets, and in server applications for managing connection loops
efficiently and cleanly.
#include <iostream>
using namespace std;
int main() {
// Menu system with break
int choice;
while(true) {
cout << "1. Play Game\n2. Settings\n3. Exit\n";
cout << "Enter choice: ";
cin >> choice;
if(choice == 3) {
cout << "Goodbye!" << endl;
break; // Exit menu
}
if(choice < 1 || choice > 3) {
cout << "Invalid choice!" << endl;
continue; // Skip to next iteration
}
cout << "You selected option " << choice << endl;
}
return 0;
}