Beginner 10 min

If-Else Statements

Learn to make decisions in your code with conditional statements and boolean logic

Learn how to make your programs intelligent by using conditional statements to make decisions based on different situations.

A Simple Example

#include <iostream>

int main() {
    int age{25};

    if (age >= 18) {
        std::cout << "You are an adult" << "\n";
    }

    // Multiple conditions with else if
    int score{85};

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

    return 0;
}

Breaking It Down

if Statement - Basic Condition

  • What it does: Executes code only when a condition is true
  • Syntax: if (condition) { code to execute }
  • Conditions use comparison operators: ==, !=, >, <, >=, <=
  • Remember: Always use == for comparison, not = which is assignment

else if - Multiple Conditions

  • What it does: Checks another condition if the previous one was false
  • Execution: Only runs if all previous conditions were false
  • Order matters: Conditions are checked top-to-bottom, first match wins
  • Remember: You can chain as many else if statements as needed

else - Default Case

  • What it does: Runs when all previous conditions are false
  • Think of it as: The "everything else" or fallback option
  • Must come last: Always appears after if and else if statements
  • Remember: else is optional - use it when you need a default action

Logical Operators - Combining Conditions

  • && (AND): Both conditions must be true - if (age >= 18 && hasLicense)
  • || (OR): At least one condition must be true - if (age < 13 || age > 65)
  • ! (NOT): Reverses the condition - if (!isStudent) means "is NOT a student"
  • Remember: Use parentheses to group complex conditions clearly

Why This Matters

  • Programs without conditionals are like robots that can only follow one path. Every meaningful program makes decisions.
  • Real software needs to respond differently based on different situations - checking user permissions, validating input, handling errors, or adapting to user choices.
  • Conditional logic is the foundation of all program control flow and decision-making.

Critical Insight

Conditions are evaluated top-to-bottom and stop at the FIRST match! This means the order of your conditions matters tremendously.

If you check score >= 60 first, a score of 100 will match that condition and never reach the "perfect score" check below it. Always order your conditions from most specific to most general.

Best Practices

Always use braces: Even for single-line conditionals, use {} to prevent bugs when adding more code later.

Order conditions from specific to general: Check the most restrictive conditions first, then broader ones.

Use comparison ==, not assignment =: The condition if (x = 5) assigns 5 to x instead of comparing. Always use == for comparison.

Keep conditions simple: Break complex conditions into separate boolean variables with clear names for readability.

Common Mistakes

Using assignment instead of comparison: Writing if (x = 5) instead of if (x == 5) assigns 5 to x and always evaluates to true.

Wrong condition order: Checking x >= 0 before x >= 100 means the second condition never executes. Order from specific to general.

Forgetting braces: Omitting {} makes only the next line conditional. Adding more code later breaks the logic.

Confusing AND/OR logic: Writing if (x == 5 || 6) does not check if x is 5 or 6. Use if (x == 5 || x == 6).

Debug Challenge

This temperature checker has a logic error. The condition order is wrong. Click the highlighted line to fix it:

1 #include <iostream>
2
3 int main() {
4 int temp{95};
5
6 if (temp > 60) {
7 std::cout << "Warm day" << "\n";
8 } else if (temp > 90) {
9 std::cout << "Very hot!" << "\n";
10 }
11
12 return 0;
13 }

Quick Quiz

  1. What does this code output if x = 10?
if (x > 15) {
    std::cout << "A";
} else if (x > 5) {
    std::cout << "B";
} else {
    std::cout << "C";
}
B
A
C
  1. What is wrong with: if (score = 100)?
Should be `score == 100`
Nothing, it is correct
Missing semicolon
  1. Which logical operator means "AND"?
`&&`
`||`
`!`

Practice Playground

Time to try out what you just learned! Play with the example code below, experiment by making changes and running the code to deepen your understanding.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once