Decision

Conditionals

Conditionals let a program make decisions. Depending on whether a condition is true or false, the program takes one path or another.

Without conditionals, a program runs the same code every time. With conditionals, it can respond differently based on data — a player’s choice, a score, an input value.

Types of Conditionals#

If/Else Statement#

The most common conditional. Checks a condition and runs one block of code if it’s true, another if it’s false.

If/Else Statement

An if/else statement lets a program make decisions. It checks a condition and runs different code depending on whether that condition is true or false.

For a broader introduction to decision-making in code, see Conditionals.

        ┌─────────────┐
        │  condition  │
        └──────┬──────┘
          true │ false
       ┌───────┴───────┐
       ▼               ▼
  ┌─────────┐     ┌──────────┐
  │ if block│     │else block│
  └─────────┘     └──────────┘

If#

The simplest form is a single if. The block runs only if the condition is true. If it’s false, nothing happens.