C++ Tutorial
Estimated reading: 4 minutes 37 views

๐Ÿ” 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, and goto fit in
  • The importance of readable and structured control logic

๐Ÿ“˜ Topics Covered

๐Ÿ”น Topic๐Ÿ“„ Description
๐Ÿ”ƒ C++ Control StatementsGrouping of all flow-controlling statements in C++
๐Ÿ”€ if / if-else StatementsConditional branching
๐Ÿงฉ Nested if StatementsConditions within conditions
๐ŸŽš๏ธ switch StatementMulti-way branching using constant values
๐Ÿ” for LoopRepeats a block a known number of times
๐Ÿ”„ while LoopRepeats a block while a condition is true
๐Ÿ”‚ do-while LoopExecutes at least once, then checks condition
๐Ÿ”ƒ foreach LoopIterates over collections using STL or ranges
๐Ÿงฌ Nested LoopsLoops inside loops for matrix/data iteration
โ›” break and continueExit or skip part of loops
๐Ÿ”€ goto StatementJumps 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-Makingif, if-else, nested if, switch, nested switch
๐Ÿ” Looping / Iterationfor, while, do-while, foreach (via STL or C++ ranges)
โ›” Jump / Branchingbreak, 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 logic
  • break, continue, and goto 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ” C++ Control Flow

Or Copy Link

CONTENTS
Scroll to Top