C++ Control Flow – Master if, switch, loops, break, and goto
Introduction – What Is Control Flow in C++?
Control flow in C++ refers to the order in which individual statements, instructions, or function calls are executed or evaluated. By default, C++ programs execute code line by line, but control flow statements allow you to make decisions, repeat operations, or alter the normal flow based on conditions or loops.
In this guide, you’ll learn:
- The key types of control flow in C++
- How to use decision-making and loops effectively
- Where
break,continue, andgotofit in - The importance of readable and structured control logic
Topics Covered
| Topic | Description |
|---|---|
| C++ Control Statements | Grouping of all flow-controlling statements in C++ |
| if / if-else Statements | Conditional branching |
| Nested if Statements | Conditions within conditions |
| 🎚️ switch Statement | Multi-way branching using constant values |
| for Loop | Repeats a block a known number of times |
| while Loop | Repeats a block while a condition is true |
| do-while Loop | Executes at least once, then checks condition |
| foreach Loop | Iterates over collections using STL or ranges |
| Nested Loops | Loops inside loops for matrix/data iteration |
| break and continue | Exit or skip part of loops |
| goto Statement | Jumps to a labeled part of code (use discouraged) |
Categories of C++ Control Flow Statements
C++ control flow can be grouped into three categories:
| Category | Statement Types |
|---|---|
| Decision-Making | if, if-else, nested if, switch, nested switch |
| Looping / Iteration | for, while, do-while, foreach (via STL or C++ ranges) |
| Jump / Branching | break, continue, goto |
1. Decision-Making Statements
These statements execute different code paths based on conditions.
if / if-else
int marks = 85;
if (marks >= 40)
cout << "Pass";
else
cout << "Fail";
Nested if
if (x > 0) {
if (x < 100) {
cout << "Positive and less than 100";
}
}
🎚️ switch / Nested switch
switch (grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Good"; break;
default: cout << "Invalid";
}
2. Looping Statements
Used to execute code repeatedly.
for Loop
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
while Loop
int i = 0;
while (i < 5) {
cout << i << " ";
i++;
}
do-while Loop
int i = 0;
do {
cout << i << " ";
i++;
} while (i < 5);
foreach Loop (STL-based)
vector<int> nums = {1, 2, 3};
for (int n : nums) {
cout << n << " ";
}
Nested Loops
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
cout << i << "," << j << endl;
}
}
3. Jump Statements
break
for (int i = 0; i < 10; i++) {
if (i == 5) break;
cout << i << " ";
}
continue
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
cout << i << " ";
}
goto
goto end;
cout << "Skipped code";
end:
cout << "Reached label";
Control Flow Example
int x = 10;
if (x > 0) {
for (int i = 0; i < x; i++) {
if (i == 5) break;
cout << i << " ";
}
}
Summary – Recap & Next Steps
Key Takeaways:
- C++ control flow governs what code runs and when
if,switch, and loop constructs help control logicbreak,continue, andgotoalter execution paths inside loops
Control flow is the engine behind decision-making and repetition in every C++ program. By mastering these constructs, you can write flexible, efficient, and structured code that reacts logically to data and conditions.
FAQs – C++ Control Flow
What is control flow in programming?
Control flow is the sequence in which statements are executed in a program.
When should I use switch over if?
Use switch when checking one variable against multiple constant values. It’s more efficient and readable.
Can I use a loop inside another loop in C++?
Yes, this is called nested loops, useful in matrices, patterns, and grids.
Is goto recommended in C++?
No. goto is discouraged as it can lead to unreadable code. Use loops and functions instead.
What’s the difference between while and do-while?
while checks the condition before execution. do-while checks it after, so it runs at least once.
Share Now :
