Jump statements alter the normal sequential flow of execution by transferring control unconditionally to another point in the program.
break vs continue#include <iostream>
int main() {
std::cout << "--- Testing Break (Stops at 5) ---\n";
for (int i = 1; i <= 10; ++i) {
if (i == 5) {
break; // Exits the loop entirely when i reaches 5
}
std::cout << i << " ";
}
// Output: 1 2 3 4
std::cout << "\n\n--- Testing Continue (Skips even numbers) ---\n";
for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0) {
continue; // Skips printing for even numbers
}
std::cout << i << " ";
}
// Output: 1 3 5 7 9
std::cout << "\n";
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Applying break and continue inside loops.
Understanding the role of break in switch-case blocks.
Returning early from functions.