Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    C++ fundamentals · Lesson 4 of 21

    Conditions: if and switch

    if/else, logical operators, switch and the ternary operator.

    9 minutes2 games

    if runs code when a condition is true, else if tries another, and else covers the rest. The condition goes in brackets and the code in { }.

    C++
    #include <iostream>
    
    int main() {
        int score = 76;
        bool submittedProject = true;
    
        if (score >= 90) {
            std::cout << "Excellent\n";
        } else if (score >= 70 && submittedProject) {
            std::cout << "Passed with project\n";
        } else if (score >= 70) {
            std::cout << "Passed\n";
        } else {
            std::cout << "Try again\n";
        }
        return 0;
    }
    OperatorMeaning

    a == b

    equals

    a != b

    not equal

    a && b

    and (both true)

    a || b

    or (at least one)

    !a

    not

    c ? x : y

    x if c is true, else y

    Important

    The classic mistake: if (x = 5) instead of if (x == 5). The first assigns 5 to x and evaluates to 5, which counts as true, so the branch always runs. The compiler warns you with -Wall, so always keep warnings on.

    switch

    C++
    #include <iostream>
    
    int main() {
        char level = 'B';
        switch (level) {
            case 'A':
                std::cout << "Advanced\n";
                break;
            case 'B':
            case 'C':
                std::cout << "Intermediate\n";
                break;
            default:
                std::cout << "Beginner\n";
        }
    
        int xp = 120;
        std::cout << (xp >= 100 ? "level up" : "keep going") << "\n";
        return 0;
    }

    switch works with integers, char and enums only, not std::string. Forget break and execution continues into the next case (fall-through). Sometimes that's useful, like 'B' and 'C' above, but usually it's a bug.

    Spot the bug

    With day = 6 the program prints two lines instead of one. Where's the problem?

    Interview question

    What is short-circuit evaluation and how do you use it?

    Exercise

    You have int n = 15. Print FizzBuzz if it divides by both 3 and 5, Fizz for 3 only, Buzz for 5 only, or the number itself.

    Expected output: FizzBuzz

    C++
    #include <iostream>
    
    int main() {
        int n = 15;
        
        return 0;
    }

    Learn more from

    • learncpp: Introduction to if statementslearncpp.com
    • cppreference: switch statementen.cppreference.com
    • cppreference: Logical operatorsen.cppreference.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousOperators and inputNextLoops
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.