LearnForFree
C++ For Beginners
Lesson

Jump & Flow Control Statements (break, continue, return)

Sometimes you need to alter the normal step-by-step execution of a loop or function early. C++ provides jump statements to give you fine-grained control over execution flow. • break: Immediately terminates the innermost loop (for, while, do-while) or switch statement it is inside. Execution jumps straight to the first line of code after that loop or switch. • continue: Skips the remaining code inside the current iteration of a loop and jumps straight to the next iteration (in a for loop, it triggers the update step i++ and re-checks the condition). • return: Immediately exits the entire function it is inside, optionally sending a value back to whoever called the function. If used inside main(), return terminates the entire program.
#include <iostream> int main() { std::cout << "Demonstrating continue and break:\n"; for (int i = 1; i <= 10; i++) { // Skip even numbers if (i % 2 == 0) { continue; // Jumps straight to i++ } // Stop the loop completely when i reaches 7 if (i == 7) { std::cout << "Hit 7! Breaking out of loop.\n"; break; // Exits the for loop entirely } std::cout << "Odd number: " << i << "\n"; } std::cout << "Loop finished!\n"; return 0; // Exits main() and ends the program }

Watch companion video on YouTube

Sign in to save your progress