C++ Programming Tutorial

Learn modern C++ step-by-step with concise explanations and practical examples. Navigate topics from basics to advanced features using the sidebar.

Control Flow

Use `if/else` for decisions, `switch` for discrete cases, and loops (`for`, `while`, range-based) for repetition.

Decisions & Loops
#include \n#include \n\nint main(){\n    int x = 5;\n    if (x > 3) { std::cout << "x > 3\n"; } else { std::cout << "x <= 3\n"; }\n\n    switch (x) {\n        case 1: std::cout << "one\n"; break;\n        case 5: std::cout << "five\n"; break;\n        default: std::cout << "other\n"; break;\n    }\n\n    for (int i = 0; i < 3; ++i) std::cout << i << " ";\n\n    std::vector v{1,2,3};\n    for (int n : v) std::cout << n << " "; // range-based\n\n    int count = 3;\n    while (count--) std::cout << "w";\n\n    return 0;\n}
Notes
  • Always `break` cases in `switch` unless you intentionally fall through.
  • Use pre-increment (`++i`) idiomatically in loops, though it rarely matters for built-ins.

Keep Practicing

Use the online compiler to run examples and test variations. Reinforce learning by building small programs for each topic.