Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

More Options: else if

With if and else, a program chooses between two options. Often there are more. A soccer match, for example, ends in one of three ways: the home team wins, the away team wins, or it's a draw. For more options, add else if branches, each with a condition of its own:

int homeGoals = 3;
int awayGoals = 2;

if (homeGoals > awayGoals) {
    std::cout << "The home team won!" << std::endl;
} else if (homeGoals < awayGoals) {
    std::cout << "The away team won!" << std::endl;
} else {
    std::cout << "It's a draw!" << std::endl;
}

This prints:

The home team won!

The program checks the conditions from top to bottom and runs the block of the first true one. It skips the rest of the branches, even if some of the later conditions are true too. Only if no condition is true does the else block run. You can add as many else if branches as you need, always between the if and the else. The else at the end is optional: without it, nothing runs when no condition is true.

Since the first true condition wins, the order of the conditions matters:

int points = 95;

if (points >= 50) {
    std::cout << "Passed" << std::endl;
} else if (points >= 90) {
    std::cout << "Excellent" << std::endl;
}

This prints:

Passed

95 points is an excellent result, but the first condition, points >= 50, is already true, so the program never gets to the second one. The branch that prints Excellent never runs here, because every number that is at least 90 is also at least 50. Put the stricter condition first: points >= 90, then points >= 50.

Instructions

  1. 1

    15 °C isn't weather for a winter coat. Between the if block and else, add an else if branch with the condition temperature > 10 that prints Wear a jacket.

  2. 2

    Add one more else if branch with the condition temperature > 18 that prints Wear a sweater, and change temperature to 20. The program should print Wear a sweater. If it prints Wear a jacket, the condition temperature > 10 comes first and catches 20 degrees too: move the new branch above it.

Back

6/12

  1. 1. The if Statement
  2. 2. Blocks and Indentation
  3. 3. The else Branch
  4. 4. Comparison Operators
  5. 5. Boolean Values
  6. 6. More Options: else if
  7. 7. Quiz: else if
  8. 8. Logical Operators: and
  9. 9. Logical Operators: or
  10. 10. Logical Operators: not
  11. 11. Nested Conditions
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.