Conditionals: Making Decisions

πŸ“Œ Concept 7 of 13✍️ Written by Mark SullivanπŸ“… Reviewed 2026-04-18⏱ ~10 min read

Conditionals are how programs make choices. Without them, code would just run top to bottom in a single straight line. With them, behavior depends on input.

Bee, our debugging mascot

From Bee: Empty arrays and empty objects are TRUTHY in JavaScript. They look falsy but they are not. Test for length explicitly.

What Is a Conditional?

A conditional runs different code depending on whether some test is true or false. The shape is universal: "if (test), do A; otherwise, do B." Almost every interesting program contains conditionals.

If / Else if / Else

For more than two paths, chain conditions:

Truthy and Falsy Values

Most languages let you put non-boolean values in an if-test. if (x) succeeds if x is "truthy." Falsy values include 0, "", null, undefined, NaN in JavaScript; 0, "", None, empty lists/dicts in Python.

Code Examples in Three Languages

let age = 17;

if (age >= 18) {
  console.log("Can vote");
} else if (age >= 16) {
  console.log("Can drive");
} else {
  console.log("Too young for both");
}

// Ternary
let status = age >= 18 ? "adult" : "minor";

// Switch (for many discrete cases)
switch (day) {
  case "Sat":
  case "Sun": console.log("Weekend"); break;
  default: console.log("Weekday");
}
age = 17

if age >= 18:
    print("Can vote")
elif age >= 16:
    print("Can drive")
else:
    print("Too young for both")

# Ternary
status = "adult" if age >= 18 else "minor"

# Match (Python 3.10+)
match day:
    case "Sat" | "Sun": print("Weekend")
    case _: print("Weekday")
int age = 17;

if (age >= 18) {
    System.out.println("Can vote");
} else if (age >= 16) {
    System.out.println("Can drive");
} else {
    System.out.println("Too young");
}

// Ternary
String status = age >= 18 ? "adult" : "minor";

// Switch
switch (day) {
    case "Sat":
    case "Sun": System.out.println("Weekend"); break;
    default: System.out.println("Weekday");
}

Best Practices

  1. Handle the most likely / important case first.
  2. Avoid deep nesting β€” use early returns or "guard clauses."
  3. Prefer many small ifs over one giant tangled condition.
  4. Always include an else for unexpected cases.

Common Mistakes

  • Using = in a condition. if (x = 5) assigns 5 and is always truthy.
  • Forgetting break in switch. Cases "fall through" without it.
  • Comparing different types loosely. Especially with JS's ==.
  • Off-by-one boundaries. < vs <= matters at the edges.
πŸ›
See the bugs in action

We have a dedicated Common Bugs with Conditionals page β€” five real broken snippets with the fix. Read it before you start writing.

How It Works Under the Hood

An if statement compiles down to a CPU branch instruction: "test this value, jump to address X if zero, otherwise continue." That branch costs the CPU prediction cycles when wrong β€” heavily-branched code in hot loops can be measurably slower than branchless equivalents. (Don't worry about this until you're writing a game engine.)

Languages differ in what counts as truthy. JavaScript: 0, "", null, undefined, NaN, false are falsy; everything else (including "false"!) is truthy. Python adds empty containers. Java is strictly boolean β€” no coercion at all.

The switch statement has fall-through behavior in C-family languages β€” without break, control falls into the next case. Python uses match (3.10+) which is pattern-matching, not classic switch.

From Beginner to Pro

The same concept gets deeper as you grow. Here's what mastery looks like at three levels:

Beginner

You write if (cond) { ... } else { ... } for two-way branches.

Intermediate

You use else if chains, ternary expressions, and switch for many discrete cases. You add an explicit else for "shouldn't happen" branches.

Pro

You use guard clauses to avoid nesting (if (!user) return). You replace deep if-trees with lookup objects or polymorphism. You handle every case explicitly so the type system catches missing branches.

Performance & Gotchas

  • Truthy strings β€” "false" and "0" are truthy in JS. Convert before checking.
  • Float equality in conditions β€” never if (a == 0.3), always tolerance.
  • Switch fall-through in C-family β€” missing break is a silent bug.
  • Java string equality β€” if (s == "x") almost always wrong. Use .equals().

Quick Quiz

Real-World Uses (Production Code, Today)

Permission checks

if (user.role === "admin") { ... } runs in some form on every "Settings" page on the internet.

Form validation

"If the email is empty, show an error" β€” that's a conditional. Stack 5 of them and you have a working form.

Pricing rules

"If the cart total is over $50, free shipping." A million-dollar e-commerce rule is a single if-statement.

Frequently Asked Questions

What's the difference between if and switch?

if checks any boolean test. switch matches a single value against many cases β€” useful when you have many discrete options.

Can conditionals be nested?

Yes β€” but try to flatten them. Deep nesting is a code smell.

What's a ternary?

A short conditional that returns a value: cond ? a : b.

What is a "guard clause"?

An early return that handles edge cases at the top of a function, leaving the main logic un-nested.

Why does if (1) execute the body?

Because 1 is "truthy." Most languages accept any value in a condition, not just booleans.

What's the difference between elif and else if?

Just syntax. Python uses elif; JS/Java use else if. Same idea.

Can I use && to chain conditions?

Yes: if (a && b) runs only if both are true. Use || for "either."

Should I always include an else?

Often, yes β€” it forces you to think about what happens in unexpected cases. Sometimes a guard-clause + return makes else unnecessary.

βœ…
Key takeaways
  • Conditionals: Making Decisions is one of the 13 universal concepts of programming.
  • The syntax differs across languages, but the underlying idea is the same.
  • Practice in the playground to make it stick.
Was this helpful?
M
Mark Sullivan
Lead writer Β· 8 yrs full-stack

Mark started coding in 2017 after switching from financial analysis. She's built production systems in Python (Django) and JavaScript (Node + React) at two startups, and has taught intro programming at his local community college since 2022. He owns the curriculum for variables, functions, conditionals, and loops on this site. More about Mark β†’