Selection Structures
Outcome 3: Selection Structures · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx
Objectives
- Write if, if/else, and if/elif/else statements that select between multiple code paths.
- Construct Boolean expressions using comparison and logical operators.
- Apply correct Python indentation to define code blocks.
- Predict the output of a selection structure given specific input values.
- Write nested if statements for multi-level decisions.
Key terms
- if statement
- A selection structure that executes its indented block only when the condition evaluates to True.
- else clause
- An optional block following an if (or if/elif chain) that executes when all conditions are False.
- elif
- Short for 'else if' — tests an additional condition when the preceding if or elif was False.
- Boolean expression
- An expression that evaluates to True or False; used as the condition in if, while, and other structures.
- comparison operator
- An operator that compares two values and returns bool: == != < > <= >=.
- logical operator
- and, or, not — combine or negate Boolean expressions to form compound conditions.
- indentation
- Whitespace (4 spaces recommended) at the start of lines that defines code block membership in Python.
- nested if
- An if statement placed inside the body of another if or else block, enabling multi-level decisions.
- short-circuit evaluation
- Python stops evaluating and with False (result is False) or or with True (result is True) without checking remaining operands.
The concept
Selection structures give programs the ability to make decisions — to choose one path or another based on conditions. Without selection, every program would do the same thing every time it ran, regardless of input. Selection is what makes programs respond differently to different situations.
The simplest selection structure is the if statement. It tests a condition; if the condition is True, the indented block runs. If the condition is False, Python skips the block and continues with the next unindented line. The condition must be a Boolean expression — something that evaluates to True or False.
The if/else structure covers two paths. One path runs when the condition is True; the other runs when it is False. The else clause has no condition — it is the 'everything else' case. These two paths are mutually exclusive: exactly one of them runs.
The if/elif/else structure handles more than two cases. Python checks each condition in order — the first one that is True executes its block, and Python skips all remaining elif and else clauses. This is critical for grade classification: check 90+ before 80+ before 70+, or the 80-89 range would never be reached if the 70+ check comes first.
Indentation is syntax in Python — it is not optional formatting. Four spaces (or one consistent tab) must begin every line in a block. Inconsistent indentation causes IndentationError or, worse, silently runs code under the wrong branch.
Boolean expressions use comparison operators (== tests equality; = is assignment; confusing them is the most common error in new programs) and logical operators. and requires both conditions true; or requires at least one; not inverts. Compound conditions: if age >= 13 and age <= 17: — both must be true to enter the block. Python also accepts chaining: if 13 <= age <= 17: — this is unique to Python and is equivalent.
Blueprint Pillar 3 — Technology and Society: Selection structures are how software enforces rules. Age verification on websites, credit eligibility checks, spam filters, and automated grading systems all reduce to if/elif/else logic. Poorly written conditions — wrong operator, missing elif, wrong order — produce the real-world bugs that let underage users access adult content or deny loans to qualified applicants.
Worked examples
Common mistakes
- Using = instead of == in a condition. if x = 5: is a SyntaxError. The equality test is ==. The assignment is =. They do different things.
- Checking conditions in the wrong order in an if/elif chain. If you write elif score >= 70 before elif score >= 80, a score of 85 matches the 70 check first and prints 'C'. Always check most specific (highest threshold) first.
- Forgetting that indentation defines the block. If the else clause is not at the same indent level as the if, Python either errors or includes it in the wrong block.
Self-check
Try each one before you look. A miss here costs nothing and tells you exactly what to reread.
Canvas is the official record. This companion enhances the PGCC curriculum; it does not replace it. Last name and class year only. Students with a 504 plan or IEP: your accommodations apply.