Programming Fundamentals

Conditionals

Branching: run one path or another based on a Boolean expression. The first decision a program can make.

Level: beginner

What is it?

A conditional evaluates a Boolean expression and runs a block only if it is true: if, else if, else, and in some languages switch or match. Combined with the logical operators and, or, and not, it lets a program choose among paths. Most languages short-circuit: in A and B, B is skipped when A is already false.

Why does it matter?

Branches are where programs differ from calculators — and where most bugs and most tests live. Each branch is a path you must reason about and test, and the number of paths grows quickly, so clear conditions are worth more than clever ones.

Mental model

A fork in the road with signs checked in order. The first sign that matches sends you down that road; the rest are never read.

How it works

  • Conditions are evaluated top to bottom. The first true branch wins and the rest are skipped.
  • Short-circuit evaluation: 'x is not None and x.ready' is safe because the second half only runs when the first is true.
  • Compare like with like: = assigns and == compares. In JavaScript prefer === to avoid type coercion.
  • Guard clauses — return early on the bad case — keep the main path flat and readable.
  • Every condition has a boundary. Test the value just below it, at it, and just above it.

Steps

  • Evaluate the condition
  • If true, run this block
  • Otherwise test the next condition
  • Fall through to else
  • Continue after the block

Code example: Guard clause, then ordered branches

def grade(score):
    if score < 0 or score > 100:
        raise ValueError("score out of range")   # guard clause
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    return "C or below"

print(grade(90), grade(89))   # A B

Real-world usage

  • Validating input before using it
  • Choosing behavior by state: logged in or out, empty or full
  • Feature flags and configuration switches

Common mistakes

  • Using = where == was meant (a syntax error in Python, a silent bug in C).
  • Ordering branches so a general case hides a specific one, such as score >= 50 before score >= 90.
  • Testing the middle of a range and forgetting its boundaries.

Interview questions

What is short-circuit evaluation and why does it matter?

In A and B the right side is evaluated only if A is true; in A or B only if A is false. It lets you guard an expression, as in 'obj is not None and obj.ready', and it avoids needless work.

Why prefer guard clauses to deep nesting?

Handling the invalid case first and returning leaves the main logic at one indentation level. Readers see the happy path without tracking a stack of open conditions.

What is the difference between == and === in JavaScript?

== converts operand types before comparing, so '1' == 1 is true. === compares type and value, so '1' === 1 is false. Prefer ===.

Prerequisites

Related concepts

Continue learning

Browse the full computer science library