Conditions
Conditional statements let a program make decisions — executing different code paths depending on whether some expression evaluates to true or false. This is the first real branching point away from a purely linear, top-to-bottom program.
A conditional statement (if, else if, else, or switch/match) runs one block of code or another depending on whether a condition is true or false. Without conditionals, a program could only ever run the exact same sequence of steps every time, regardless of input.
Real programs must react differently to different situations — a login form must behave differently for a correct password than an incorrect one; a game must behave differently when a player wins versus loses. Conditionals are the fundamental mechanism for that kind of branching logic.
Validating a form field, deciding which page to show a logged-in versus logged-out user, checking a sensor reading against a safety threshold in embedded firmware, deciding tax brackets in a financial calculation — conditionals appear in essentially every nontrivial program.
A conditional is like a fork in a hiking trail with a sign: 'if the weather is sunny, take the left path; otherwise, take the right path.' You always reach the fork, but which path you actually walk depends entirely on the current condition (the weather) at that moment.
The program checks whether age is greater than or equal to 18. Since it is, it runs the print("Adult") branch and skips the else branch entirely.
if/else if/else chains are evaluated top to bottom, and only the first matching branch executes — even if a later condition would also be true, it's never even checked once an earlier branch matches.
A switch (or match, in newer languages) statement compares one value against several possible cases — often more readable than a long if/else if chain when checking one variable against many discrete possibilities, though the two are logically equivalent in most cases.
The ternary/conditional operator (condition ? valueIfTrue : valueIfFalse in C-family languages) is a compact expression form of if/else that produces a value directly, rather than a statement that merely chooses which block to run.
Truthy and falsy values: many dynamically typed languages (JavaScript, Python) let non-boolean values be used directly in a condition, implicitly converting them to true or false (0, empty string, None/null/undefined are typically falsy; most other values are truthy) — a convenience that also causes real bugs when the implicit conversion isn't what the programmer expected.
Writing overlapping or unreachable conditions in an if/else if chain, where an earlier, broader condition accidentally 'shadows' a later, more specific one that can then never be reached.
Forgetting break statements in a switch statement in C-family languages, causing execution to unintentionally 'fall through' into the next case's code.
Relying on truthy/falsy coercion without realizing which specific values count as falsy in that language — for instance, an empty array is truthy in JavaScript but an empty list is falsy in Python, a subtle cross-language trap.
Writing deeply nested if statements ("arrow code") that become difficult to read, when early returns or restructuring the logic could flatten it considerably.
Using a switch/match statement instead of a long if/else if chain when checking a single variable against many known discrete values, purely for readability.
Using early returns at the top of a function to handle invalid or edge-case inputs immediately, avoiding deeply nested conditional logic for the main case.
Using the ternary operator for short, simple value selection (e.g. const label = isActive ? "On" : "Off";), while avoiding it for anything complex enough to hurt readability.
Branching at the machine-code level A high-level if statement typically compiles down to a comparison instruction followed by a conditional jump/branch instruction that redirects the CPU's instruction pointer based on the comparison's result. Modern CPUs use branch prediction to guess which way a branch will go before it's actually resolved, and a mispredicted branch causes a pipeline flush with a real, measurable performance cost — covered fully in the future Computer Architecture module.
Pattern matching as a more powerful conditional Languages like Rust, Haskell, and modern versions of Python (match statements) generalize switch-style branching into pattern matching — not just comparing a value against constants, but also destructuring compound data (tuples, enums with associated data) and binding parts of it to new variables directly within the branch condition, often with compiler-enforced exhaustiveness checking (guaranteeing every possible case is handled).
Short-circuiting conditions and side effects Because if conditions are themselves expressions built from operators, the same short-circuit evaluation rules discussed under Operators & Expressions directly affect which parts of a compound condition actually execute — a subtlety that matters when a condition includes a function call with a meaningful side effect.
What's the difference between if/else if/else and a switch statement?
They're logically similar — both choose one branch from several — but a switch statement compares one single value against several discrete cases, generally more readable for that specific pattern, while if/else if chains can test entirely independent, arbitrary conditions in each branch.
Output-based: in JavaScript, what does if ([]) console.log("truthy"); print, given an empty array?
It prints "truthy" — in JavaScript, an empty array is a truthy value (only a specific short list of values, like 0, "", null, undefined, NaN, and false, are falsy). This differs from Python, where an empty list is falsy.
What is fall-through in a switch statement, and why is it usually avoided?
Fall-through happens in C-family switch statements when a case block doesn't end with a break, so execution continues into the next case's code regardless of whether that case's value matches — usually unintended and a common source of bugs, though occasionally used deliberately to group cases together.
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")