Study Notes: AP CSP Control Structures
Pseudocode (AP Style)
Loops and Random
Iteration: For loops
Overview
For loops are specific types of iteration that repeat code blocks a fixed number of times.
Pseudocode Syntax
Structure:
REPEAT n TIMES
{
}
The block of statements within the for loop executes exactly n times.
Example in Pseudocode
The following pseudocode initializes a sum variable and computes the sum of numbers from 0 to 4:
Pseudocode:
sum ← 0
count ← 0
repeat 5 times {
sum ← sum + count
count ← count + 1
}
DISPLAY(count)
Python Example
Python Code:
sum = 0
for i in range(5):
sum += i
print(sum)
This code initializes a variable sum, iterates 5 times adding the loop variable to sum, and finally prints the sum.
Iteration: While Loops
Overview
While loops continue to execute the block of statements as long as the provided Boolean expression evaluates to true.
Pseudocode Syntax
Structure:
REPEAT UNTIL (condition)
{
}
Example in Pseudocode
The following pseudocode initializes
numand decreases it until it is less than zero:Pseudocode:
num ← 5
repeat until num < 0 {
print(num)
num ← num - 1
}
OUTPUT 5 4 3 2 1 0
Python Example
Python Code:
num = 5
while num >= 0:
print(num)
num = num - 1
This code initializes a numeric variable and prints it until it is negative, demonstrating a countdown.
Comparison of Programs A and B
Program A Analysis
Code Structure:
REPEAT UNTIL (i > 10)
DISPLAY i
i ← i + 1
Output Values:
Program A displays the values: 1 2 3 4 5 6 7 8 9 10.
Program B Analysis
Code Structure:
REPEAT UNTIL (i > 10)
i ← i + 1
DISPLAY i
Output Values:
Program B also displays the values: 1 2 3 4 5 6 7 8 9 10.
Both programs display the same sequence of numbers despite the difference in their structure.
Random: Coin Flip Game
Game Description
Players flip a fair coin three times. The winning condition is that all flips must result in the same outcome (either all heads or all tails).
If this is achieved, the player wins; otherwise, they lose.
Pseudocode for Coin Flip Game
flip ← RANDOM(0,3)
IF (flip = 3):
DISPLAY "You win!"
ELSE:
DISPLAY "You lose."
sum ← 0
REPEAT 3 TIMES:
sum ← sum + RANDOM(0,1)
IF (sum = 3):
DISPLAY "You win!"
ELSE:
DISPLAY "You lose."
Random: Spinner
Spinner Probability Example
The probability of the spinner landing on different colors is illustrated as follows:
For a single color (Pink):
Pseudocode Example:
```pseudocode
spin ← RANDOM(1,6)
if spin = 1:
DISPLAY("Pink")
else:
DISPLAY("Not Pink")
- For another color (Green):
- **Pseudocode Example:**
```pseudocode
spin ← RANDOM(1,6)
if spin = 1 or spin = 2:
DISPLAY("Green")
else:
DISPLAY("Not Green")
Spinner Design
The spinner design includes:
A blue section which occupies ½ (1/2) of the spinner.
Equal-sized orange and purple sections.
Pseudocode for Spinner Outcomes
IF RANDOM(1,4) = 1:
DISPLAY "blue"
ELSE:
IF RANDOM(1,2) = 1:
DISPLAY "orange"
ELSE:
DISPLAY "purple"
Vocabulary
Sequencing
Selection
Iteration
Abstraction
Control Structure
Sequential Programming
Arithmetic Operator
Logical Operators
Comparison Operator
Truth Table
To Do In Class
Control Structures Lab
Catch up on Code HS
Complete Code HS 4.12 Practice Quiz