If/Else Statements Lesson

Code Runner Challenge

If/Else Statements Lesson

View IPYNB Source
%%js

// CODE_RUNNER: If/Else Statements Lesson

let health = 15;
let damage = 20;

health = health - damage;

if (health <= 0) {
  console.log("Game over");
} else {
  console.log("Keep playing with " + health + " health left");
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
An if/else statement lets a program choose between different actions. The if block runs when the condition is true. The else block runs when the condition is false. This gives the program a way to respond instead of doing the same thing every time. The code subtracts damage first, then checks the new health. That order matters because the decision should be based on the updated value. The condition inside the parentheses should produce a boolean result. Comparisons like score >= 100 or health <= 0 are common because they turn a value into a true-or-false decision.