⬅ Previous Next ➡

Control Flow in C++

Control Flow in C++
  • Control Flow decides the order in which statements execute in a C++ program.
  • C++ provides decision making statements (if, if-else, nested if, switch) and looping statements (for, while, do-while).
  • Jump statements change normal flow: break, continue, goto, return.

1) if Statement

  • Executes a block only when condition is true.
#include <iostream>
using namespace std;

int main() {
    int marks = 75;

    if (marks >= 40) {
        cout << "Result: Pass" << endl;
    }

    return 0;
}

2) if-else Statement

  • Runs if block when condition is true, else runs else block.
#include <iostream>
using namespace std;

int main() {
    int marks = 35;

    if (marks >= 40) {
        cout << "Result: Pass" << endl;
    } else {
        cout << "Result: Fail" << endl;
    }

    return 0;
}

3) Nested if

  • Used when multiple conditions are needed.
#include <iostream>
using namespace std;

int main() {
    int marks = 86;

    if (marks >= 40) {
        if (marks >= 75) {
            cout << "Grade: Distinction" << endl;
        } else {
            cout << "Grade: Pass" << endl;
        }
    } else {
        cout << "Grade: Fail" << endl;
    }

    return 0;
}

4) switch-case Statement

  • Used when one variable is compared with multiple constant cases.
  • break avoids fall-through.
#include <iostream>
using namespace std;

int main() {
    int choice = 2;

    switch (choice) {
        case 1:
            cout << "Menu: Start" << endl;
            break;
        case 2:
            cout << "Menu: Result" << endl;
            break;
        case 3:
            cout << "Menu: Exit" << endl;
            break;
        default:
            cout << "Invalid Option" << endl;
    }

    return 0;
}

5) for Loop

  • Best when number of iterations is known.
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << "Count: " << i << endl;
    }
    return 0;
}

6) while Loop

  • Condition is checked first, then loop runs.
#include <iostream>
using namespace std;

int main() {
    int n = 3;

    while (n > 0) {
        cout << "Attempts left: " << n << endl;
        n--;
    }

    return 0;
}

7) do-while Loop

  • Runs at least once (condition checked after body).
#include <iostream>
using namespace std;

int main() {
    int i = 1;

    do {
        cout << "Run: " << i << endl;
        i++;
    } while (i <= 3);

    return 0;
}

8) Jump Statements

8.1) break (Exit Loop/Switch)
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) break;
        cout << i << " ";
    }
    cout << endl;
    return 0;
}
8.2) continue (Skip Current Iteration)
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 6; i++) {
        if (i == 3) continue;
        cout << i << " ";
    }
    cout << endl;
    return 0;
}
8.3) goto (Use Carefully)
  • Not recommended in large programs, but may appear in theory.
#include <iostream>
using namespace std;

int main() {
    int n = 1;

start:
    cout << "n = " << n << endl;
    n++;

    if (n <= 3) goto start;

    return 0;
}
8.4) return (Exit Function)
#include <iostream>
using namespace std;

int main() {
    int age = 15;

    if (age < 18) {
        cout << "Access Denied" << endl;
        return 0;
    }

    cout << "Access Granted" << endl;
    return 0;
}

9) Quick Notes

  • Use if for conditions and switch for fixed options.
  • Use for when iterations are known; while when unknown.
  • do-while guarantees at least one execution.
  • break exits loop/switch; continue skips iteration.
⬅ Previous Next ➡