...

The else Clause

Now that you know how to use if statements, let's learn about the else clause. You can add an else clause after any if statement:

if (condition) {
  // This code runs when condition is true
}
else {
  // This code runs when condition is false
}

This is how the computer executes an if/else statement:

  1. It first evaluates the condition
  2. If the condition is true, it runs the if block
  3. If the condition is false, it runs the else block

Try It Out

Run the code sample in the editor and try different scores to see how the if/else statement chooses which message to display. Notice that you always get exactly one message, no matter what score you enter.

Loading...

Output (Console)