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.

int score = 10;

if (score == 10)
{
    Console.WriteLine("You got a perfect score!");
}
score = 10

if score == 10:
    print("You got a perfect score!")
let score = 10;

if (score === 10) {
    console.log("You got a perfect score!");
}

If/Else#

Adding else gives the program a fallback. If the condition is false, the else block runs instead. Only one block ever runs — never both.

int score = 7;

if (score == 10)
{
    Console.WriteLine("You got a perfect score!");
}
else
{
    Console.WriteLine("You didn't get a perfect score.");
}
score = 7

if score == 10:
    print("You got a perfect score!")
else:
    print("You didn't get a perfect score.")
let score = 7;

if (score === 10) {
    console.log("You got a perfect score!");
} else {
    console.log("You didn't get a perfect score.");
}

Else If#

When there are more than two possible outcomes, chain conditions with else if. Each condition is checked in order — the first one that is true runs, and the rest are skipped.

int score = 7;

if (score == 10)
{
    Console.WriteLine("Perfect score!");
}
else if (score >= 5)
{
    Console.WriteLine("Good score.");
}
else
{
    Console.WriteLine("Keep practicing.");
}
score = 7

if score == 10:
    print("Perfect score!")
elif score >= 5:
    print("Good score.")
else:
    print("Keep practicing.")
let score = 7;

if (score === 10) {
    console.log("Perfect score!");
} else if (score >= 5) {
    console.log("Good score.");
} else {
    console.log("Keep practicing.");
}

The final else is optional — it acts as a catch-all if none of the conditions above it are true.

Conditions#

Conditions are built using comparison operators. See Comparison Operators for the full list.

Common Mistakes#

Using = instead of == score = 10 assigns the value 10 to the variable score. score == 10 checks if score equals 10. Using = inside a condition is either a compile error or causes unexpected behaviour depending on the language.

Expecting both blocks to run Only one branch ever executes. The first condition that is true wins — the rest are skipped entirely.

Wrong boundary in condition Writing score > 5 when you meant score >= 5 silently excludes the boundary value. Always test the exact boundary when checking ranges.

Adding else without thinking Not every if needs an else. Only add it when there is meaningful code to run in the false case.

Checking conditions in the wrong order In an else if chain, the first matching condition wins. If a broader condition appears before a more specific one, the specific case will never be reached.

Resources#