C++ Programming Tutorial

Learn modern C++ step-by-step — from basics to advanced features like templates, STL, and smart pointers. Clear explanations with practical, runnable examples.

C++ Control Flow

Control flow statements direct the order of execution. C++ provides conditional statements (if, switch), loops (for, while, do-while, range-based for), and jump statements (break, continue, return).

if / else if / else

conditionals.cpp
#include <iostream>

int main() {
    int score = 85;

    if (score >= 90) {
        std::cout << "Grade: A
";
    } else if (score >= 80) {
        std::cout << "Grade: B
";
    } else if (score >= 70) {
        std::cout << "Grade: C
";
    } else {
        std::cout << "Grade: F
";
    }

    // Ternary operator for simple conditions
    std::string status = (score >= 60) ? "Pass" : "Fail";
    std::cout << status << "
";

    // C++17: if with initializer
    if (auto len = status.size(); len > 3) {
        std::cout << "Long status: " << status << "
";
    }

    return 0;
}

switch Statement

switch.cpp
#include <iostream>

int main() {
    int day = 3;

    switch (day) {
        case 1: std::cout << "Monday
"; break;
        case 2: std::cout << "Tuesday
"; break;
        case 3: std::cout << "Wednesday
"; break;
        case 4: std::cout << "Thursday
"; break;
        case 5: std::cout << "Friday
"; break;
        case 6:
        case 7: std::cout << "Weekend
"; break;  // fallthrough for 6 and 7
        default: std::cout << "Invalid
"; break;
    }

    // C++17: switch with initializer
    switch (int x = compute(); x) {
        case 0: handle_zero(); break;
        default: handle_other(x); break;
    }

    return 0;
}

for Loop

for_loops.cpp
#include <iostream>
#include <vector>
#include <string>

int main() {
    // Classic for loop
    for (int i = 0; i < 5; ++i) {
        std::cout << i << " ";  // 0 1 2 3 4
    }
    std::cout << "
";

    // Counting backwards
    for (int i = 10; i > 0; --i) {
        std::cout << i << " ";  // 10 9 8 ... 1
    }
    std::cout << "
";

    // Range-based for (C++11) — preferred for containers
    std::vector<std::string> fruits{"apple", "banana", "cherry"};
    for (const auto& fruit : fruits) {
        std::cout << fruit << "
";
    }

    // Range-based with index (C++20 or manual)
    for (int i = 0; const auto& f : fruits) {
        std::cout << i++ << ": " << f << "
";
    }

    return 0;
}

while and do-while

while_loops.cpp
#include <iostream>

int main() {
    // while — check condition first
    int count = 5;
    while (count > 0) {
        std::cout << count << " ";
        --count;
    }
    std::cout << "
";  // 5 4 3 2 1

    // do-while — execute at least once
    int input;
    do {
        std::cout << "Enter positive number: ";
        std::cin >> input;
    } while (input <= 0);  // repeats until valid

    std::cout << "You entered: " << input << "
";
    return 0;
}

break, continue, and Early Return

jump.cpp
#include <iostream>
#include <vector>

int main() {
    // break — exit loop immediately
    std::vector<int> nums{1, 5, -3, 8, 2};
    for (int n : nums) {
        if (n < 0) {
            std::cout << "Found negative: " << n << "
";
            break;
        }
        std::cout << n << " ";
    }
    // Output: 1 5 Found negative: -3

    // continue — skip to next iteration
    for (int i = 0; i < 10; ++i) {
        if (i % 2 == 0) continue;  // skip even numbers
        std::cout << i << " ";  // 1 3 5 7 9
    }
    std::cout << "
";

    return 0;
}

Best Practices

  • Use range-based for when iterating over containers — it is safer and clearer than index-based loops.
  • Always use braces with if/else, even for single statements, to prevent bugs from later edits.
  • Prefer ++i over i++ in loops — for iterators it avoids creating a temporary copy.
  • Always break in switch cases unless intentional fallthrough (mark with [[fallthrough]] in C++17).
  • Use if with initializer (C++17) to limit variable scope to the condition block.
  • Avoid infinite loops without a clear exit condition — always ensure the loop variable changes.

Keep Practicing

Use the online compiler to run every example and experiment with modifications. The best way to learn C++ is by writing code — even small programs build strong foundations.