All Subjects



AP Comp Sci P



Big Idea 3: Algorithms and Programming


3 min read•november 16, 2020


Minna Chow

AP Computer Science Principles ⌨️

Bookmarked 2.3k • 69 resources
See Units

Iterative statements are also called loops, and they repeat themselves over and over until the condition for stopping is met.
There are two types of loops.
In College Board's Pseudocode, the first is a REPEAT n TIMES loop, where the n represents some number.
n can either be a number outright or some variable. The loop could state outright "REPEAT 5 TIMES," for example, but it's more likely that you'll see something like this:
n = 5
print (n)
In Python, the closest thing in comparison is a for... in range loop.
for x in range (0, 6): print (x) #The range includes 0 but not 6, so this loop runs 5 times #This loop can also be written as... for x in range (6): print (x)
The second type of loop is a REPEAT UNTIL (condition) loop, where the loop will continue to run until a condition is met.
n Python, the main form of loop that operates based on a condition is known as a while loop. Unlike a REPEAT UNTIL loop, while loops run "while" a condition is met and end when that condition is no longer true.
tacos = 5 while tacos > 0: print ("Nom Nom") tacos = tacos - 1 #Note: tacos = tacos - 1 can also be written as tacos -= 1. #The same goes for addition (tacos += 1)
❓ How many times will this loop print "Nom Nom?"
The answer is 5 times!

Iteration Practice Problem

Can you think of a way to write this loop so that it matches the REPEAT UNTIL format?
tacos = 5 while not tacos == 0: print ("Nom Nom") tacos = tacos - 1
Using the NOT operator, we can set it so that this loop will continue to run until the tacos variable equals 0.
Notice how, in both of these examples, there's the tacos = tacos - 1 statement after the print statement? This is to reduce the value of the tacos variable. Once tacos is less than 1, the loop stops running.
If we didn't have this statement, the loop would continue repeating forever (or, at least, until your computer ran out of power or resources). These repeating loops are known as infinite loops.
❗If the condition at the beginning of a REPEAT UNTIL loop is already met when you run the loop in your program, the loop won't execute at all.
tacos = 0 while tacos > 0: print ("Nom Nom") tacos = tacos - 1
In the above example, the loop won't run because tacos already equals 0.

More Operators in Loops

When writing conditions for loops, you can also use the AND and OR operators.
Here's an example of one...
tacos = 5 avocados = 5 while tacos > 0 and avocados > 0: print ("Nom Nom") tacos = tacos - 1 avocados = avocados - 1 #technically, you don't need this last line; if just tacos = 0, the loop wouldn't run
and the other!
tacos = 5 quesadillas = 10 while tacos > 0 or quesadillas > 0: print ("Nom Nom") tacos = tacos - 1 quesadillas = quesadillas - 1 #this program will print "Nom Nom" 10 times. More food for everyone!

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🕹Big Idea 1: Creative Development
⚙️Big Idea 2: Data
📱Big Idea 3: Algorithms and Programming
🖥Big Idea 4: Computer Systems and Networks
⌨️Big Idea 5: Impact of Computing
📝Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
💪🏽 Are you ready for the AP CSP exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!