๐ 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
, andgoto
fit 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
, andgoto
alter 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 :