Variables & Control Structures - Complete Interactive Lesson
Part 1: Core Concepts
🔧 Variables & Control Structures
Part 1 of 7 — Variables, Assignments, Conditionals, and Loops
Variables and Assignment
In the AP CSP pseudocode:
score ← 0 // Assign 0 to score
name ← "Alice" // Assign "Alice" to name
score ← score + 10 // Update score (now 10)
| Concept | Pseudocode | Meaning |
|---|---|---|
| Assignment | x ← 5 | Store 5 in variable x |
| Update | x ← x + 1 | Add 1 to current value of x |
| Display | DISPLAY(x) | Show value of x on screen |
🔑 The left arrow (←) means "gets the value of." It is NOT an equality check — it is an assignment. The right side is evaluated first, then stored in the left side.
Data Types
| Type | Examples | Operations |
|---|---|---|
| Number | 42, 3.14, -7 | +, -, *, /, MOD |
| String | "hello", "123" | Concatenation, length |
| Boolean | true, false | AND, OR, NOT |
| List | [1, 2, 3] | Access, insert, append, remove |
Concept Check 🎯
Conditionals (Selection)
IF (condition)
{
// Runs if condition is true
}
ELSE
{
// Runs if condition is false
}
Comparison Operators
| Operator | Meaning |
|---|---|
| = | Equal to |
| ≠ | Not equal to |
| > | Greater than |
| < | Less than |
| ≥ | Greater than or equal to |
| ≤ | Less than or equal to |
Boolean Operators
| Operator | Description |
|---|---|
| AND | True only if BOTH are true |
| OR | True if at LEAST one is true |
| NOT | Reverses true/false |
Loops (Iteration)
REPEAT n TIMES
REPEAT 5 TIMES
{
DISPLAY("Hello")
}
// Displays "Hello" five times
REPEAT UNTIL (condition)
x ← 1
REPEAT UNTIL (x > 5)
{
DISPLAY(x)
x ← x + 1
}
// Displays: 1, 2, 3, 4, 5
Applied Recall ✍️
-
The assignment operator in AP CSP pseudocode is the _______ arrow (←).
-
The Boolean operator that returns true only when BOTH conditions are true is _______.
-
REPEAT UNTIL checks the condition _______ each iteration (not after).
Evaluate the Expression 🔍
AP Exam Strategy: Variables & Control
- Trace code carefully — write down variable values after each line
- Assignment (←) is NOT equality — the right side is evaluated first
- Know De Morgan: NOT(A AND B) = NOT A OR NOT B; NOT(A OR B) = NOT A AND NOT B
- REPEAT UNTIL is like a while loop that continues UNTIL the condition becomes true (opposite of while)
- Every IF needs a corresponding condition; nested IFs test conditions in order
- Swapping two variables requires a temp variable: temp ← a, a ← b, b ← temp
AP-Style Application 🎯
Part 2: Key Processes
🔀 Variables & Control Flow
Part 2 of 7 — Key Processes
Variables Are Named Containers For Values
A variable is a name bound to a value in memory. Programs use variables to store, retrieve, and update information as they run.
| Statement | Effect |
|---|---|
| score ← 0 | Create variable score, set to 0. |
| score ← score + 10 | Read current score, add 10, store back. |
| name ← "Alex" | Bind a string to name. |
The arrow ← (or = in many languages) is assignment, not equality.
Concept Check 🎯
Data Types A Variable Can Hold
| Type | Example |
|---|---|
| Integer | 42, -7 |
| Floating-point | 3.14, -0.5 |
| Boolean | true, false |
| String | "hello" |
| List | [1, 2, 3] |
Part 3: Patterns & Examples
🔀 Variables & Control Flow
Part 3 of 7 — Patterns & Examples
Common Control-Flow Patterns
| Pattern | Skeleton |
|---|---|
| Counter | i ← 0; REPEAT n TIMES: i ← i + 1 |
| Accumulator | total ← 0; FOR EACH x: total ← total + x |
| Search | found ← false; FOR EACH x: IF x = target THEN found ← true |
| Filter | result ← []; FOR EACH x: IF condition(x) THEN APPEND(result, x) |
| Max / min | best ← list[0]; FOR EACH x: IF x > best THEN best ← x |
Concept Check 🎯
Selection Patterns
| Pattern | When |
|---|---|
| IF / ELSE | Two branches. |
| IF / ELSE IF / ELSE | Multiple ranked conditions. |
| Guard | Early return on edge case. |
Part 4: Connections & Interactions
🔀 Variables & Control Flow
Part 4 of 7 — Connections & Interactions
Variables & Control Connect Across CSP
| Connection | Why |
|---|---|
| Variables ↔ Data | Variables hold data values. |
| Control flow ↔ Algorithms | Algorithms ARE control flow over data. |
| Variables ↔ Procedures | Local variables vs. global state. |
| Control ↔ Events | Event-driven code is control flow triggered externally. |
Concept Check 🎯
Scope Of A Variable
Scope = the part of the program where a variable name is visible.
| Scope | Lifetime |
|---|---|
| Local | Inside a procedure call only. |
| Global | Whole program; persists. |
| Block | Inside an IF / loop block (some languages). |
Why scope matters: tightly scoped variables prevent unrelated code from accidentally reading or modifying them.
Mutability
Part 5: Change Over Time
🔀 Variables & Control Flow
Part 5 of 7 — Change Over Time
How Programming Languages Handle Variables Has Evolved
| Era | Trend |
|---|---|
| 1970s | Static typing (C, Pascal); declare type up front. |
| 1990s | Dynamic typing (Python, JavaScript); flexibility. |
| 2010s | Type inference + optional types (TypeScript, Rust). |
| 2020s | Stronger compile-time guarantees, immutability by default in many new languages. |
Concept Check 🎯
Static vs. Dynamic Typing
| Style | When type is checked |
|---|---|
| Static | Compile time (C, Java, Rust). |
| Dynamic | Run time (Python, JS). |
| Gradual | Optional annotations (TypeScript, mypy). |
Static = catch errors earlier; Dynamic = faster prototyping; Gradual = mix-and-match.
Concurrency Pressure On Variables
Modern multi-core hardware made shared mutable state risky. New languages (Rust, Go, modern Java) provide tools to make concurrent variable access safer.
AP CSP Pseudocode Style
Part 6: Problem-Solving Workshop
🔀 Variables & Control Flow
Part 6 of 7 — Problem-Solving Workshop
Variables & Control Workshop
Trace and design with vocabulary in hand.
Concept Check 🎯
Worked: Trace A Loop
sum ← 0 FOR i FROM 1 TO 4: sum ← sum + i
| Iter | i | sum (after) |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
Final sum = 10.
Worked: Conditions
IF score ≥ 90 THEN grade ← "A" ELSE IF score ≥ 80 THEN grade ← "B" ELSE IF score ≥ 70 THEN grade ← "C" ELSE grade ← "F"
For score = 85 → grade = "B".
Worked: Off-By-One Caution
FOR i FROM 1 TO LENGTH(list): PROCESS(list[i])
In a 1-indexed list this is fine; in a 0-indexed language, you'd need 0 to LENGTH(list) - 1. AP pseudocode is 1-indexed.
Applied Recall ✍️
-
In AP CSP pseudocode, the first element of a list is at index _______.
Part 7: AP Review
🔀 Variables & Control Flow
Part 7 of 7 — AP Review
AP Exam Recap — Variables & Control Flow
Final review.
Concept Check 🎯
Final Vocab
| Term | Definition |
|---|---|
| Variable | Named container for a value. |
| Assignment | Set a variable's value. |
| Boolean | True / false. |
| Sequencing | Steps in order. |
| Selection | IF / ELSE. |
| Iteration | Loop. |
| Scope | Where a variable is visible. |
| Local / global | Inside a procedure / whole program. |
| Constant | Variable that never changes. |
| Off-by-one | A common bug at loop boundaries. |
| Side effect | Change to state outside the local scope. |
Common Pitfalls
- Reading = as equality when it's assignment (or vice versa).