StayTalentReady

Selection Structures

Outcome 3: Selection Structures · Blueprint Pillar 3 · PGCC INT-1700 (interim) · Download .docx

Objectives

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

Example 1: Grade classifier: score = int(input('Enter score: ')). if score >= 90: print('A') elif score >= 80: print('B') elif score >= 70: print('C') elif score >= 60: print('D') else: print('F'). Trace with score=82: 82>=90? No. 82>=80? Yes → print('B'). Stop. Correct. Note: order matters — if you wrote elif score >= 70 BEFORE elif score >= 80, score=85 would print 'C' instead of 'B'.
Example 2: Compound condition — login check: if username == 'admin' and password == 'pass123': print('Welcome') else: print('Access denied'). Both conditions must be True for access. If either is wrong, access is denied. Short-circuit: if username != 'admin', Python never evaluates the password condition.

Common mistakes

Self-check

Try each one before you look. A miss here costs nothing and tells you exactly what to reread.

1. In Python, which symbol tests if two values are equal inside a condition?
2. What output does the following produce when x = 5? if x > 10: print('A') elif x > 3: print('B') else: print('C')
3. Which logical operator returns True ONLY when both conditions are True?
4. What is the minimum number of spaces Python requires for indentation inside an if block?
5. An if/elif/else chain executes how many of its branches when run?

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.

← Variables, Data Types, and OperatorsIteration and Loops →

↑ Back to top