Computational Thinking · Week 2 — Iteration & selection
1026 words
5 min read
2026-08-16T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
accumulation, max/min, AND predicates — concepts, pattern families, and traps for Quiz 2 week 2. # Week 2 — iteration & selection > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 2 — iteration & selection
Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-ct-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.
Week map
Loop over collection → accumulate → compare for max → filter with AND
Classify → Represent → Execute → Trap-check
- Recognize: Ask: Initial value for product accumulation?
- Procedure: Initialize total 0. For each x, add to total. Result after last item.
- Variations / traps: Watch for: Max of empty without check.
Formula chain (compressed)
accumulator → max/min scan → AND predicate over loop → selection diamond.
- Accumulator —
sum ← sum + x— running total - Max/min —
if x > best: best ← x— linear extremum - AND over items —
all satisfy P(x)— flag stays true - Selection —
if condition → branch— at most one path - Counter —
count ← count + 1— how many match
Open interactive formula desk · Week 2 tab.
Deep study
Computational Thinking · Week 2 — Iteration and selection
Loop patterns combine walking data with filters (AND predicates) and aggregates (sum, max, count).
Week map
Loop over collection → neutral start value → accumulate → scan for max/min → filter with AND → empty input edge cases.
Accumulation notation
- Sum accumulator → start 0 →
total ← total + x. - Product accumulator → start 1 →
prod ← prod * x. - Count accumulator → start 0 → increment when condition holds.
- Max scan → start first item or sentinel → replace when
x > best.
AND filter
All conditions must pass:
“positive and even” →
x > 0 and x % 2 == 0.Contrast OR (week 3 procedures): “positive or even” passes more items.
Mini-list:
[3, -4, 6, 8, -2]. Positive and even → only 6, 8.Max and min scans
textbest ← first item for each x in data: if x > best: best ← x
Empty
data: no first item — algorithm must guard or define error.Mini-example:
[5, 2, 9, 9, 1]. Max scan ends 9. For positive max only: ignore nonpositive, start best at first positive or None.Pattern families
Easy — Sum or count list
- Sum all elements.
- Count how many equal a target.
- Product of list (start 1).
Medium — Max with AND filter
- Largest among entries meeting two tests.
- Count items where both predicates true.
- Sum only negatives in list.
Hard — Compound loop logic
- Multiple accumulators in one pass (sum and count together).
- Empty list: max undefined; count returns 0.
- Index loop vs for-each — same totals if bounds correct.
Worked mini-examples
Example 1 — Sum.
Data
[10, 20, 5]. total=0 → 10 → 30 → 35.Example 2 — AND count.
Data
[2, 3, 4, 5, 6]. Count even and >3:- 4 yes, 6 yes → count 2.
Example 3 — Filtered max.
Data
[-1, 8, 3, 12, 5]. Max among positive:Candidates 8,3,12,5 → max 12.
Example 4 — Empty.
Data
[]. Sum loop leaves total=0. Max scan without guard — undefined; safe design returns sentinel or “no data”.Example 5 — Dual accumulator.
Data
[1,2,3,4]. Track sum and count of evens in one pass:Evens 2,4 → sum 6, count 2.
Traps
- Product accumulator starting at 0 (always 0).
- OR filter when problem says “all conditions”.
- Max of empty without check.
- Off-by-one manual index:
0..len-1not0..len. - Updating max before checking filter — polluted by ineligible items.
Diagnostic (try yourself)
-
List
[7, -2, 4, 0, 11]. What is the sum of positive entries only? -
Same list: how many entries are both positive and less than 10?
-
List
[3, 9, 1, 9, 2]. What is the maximum value? If we only consider values ≥ 5, what is the max? -
Why does product accumulation start at 1, not 0?
-
One pass: for
[5, 10, 15, 20], findsumandcountof multiples of 5.
ChatGPT prep archive
Archived import for extra depth — complements the notes above, not official IITM material.
Core concepts
- Accumulation: start neutral (0 for sum, 1 for product) then combine each item.
- Max/min scan: keep best-so-far; compare each element.
- Selection with AND: all conditions must pass—think checklist.
- Loop guard: ensure collection non-empty before picking first as max.
Notation & vocabulary
| Goal | Start value |
|---|---|
| sum | 0 |
| product | 1 |
| max | first item or sentinel |
Pattern families
Easy — Sum list
Initialize total 0. For each x, add to total. Result after last item.
Medium — Max with AND filter
Only consider items where predicate true (e.g. positive AND even). Update max among eligible only.
Hard — Count satisfying all
Nested logic: loop with compound condition incrementing counter when all tests pass. Empty input yields 0 count, not error.
Drill these on the pattern atlas — filter to week 2.
Traps
- Max of empty without check.
- OR used when problem says all conditions.
- Accumulator wrong neutral element.
- Off-by-one in manual index loop.
Retrieval prompts
- Initial value for product accumulation?
- How does AND filter differ from OR?
- What if max is sought on empty data?
Practice loop
- Read Deep study (if present) or core concepts once.
- Recite the formula chain without looking.
- Open one easy pattern on the interactive atlas for week 2.
- Attempt without solutions; mark studied after an honest try.
- Say one trap aloud before closing the tab.