πŸ“š

Β >Β 

⌨️ 

Β >Β 

πŸ“±

3.8 Iteration

3 min readβ€’january 3, 2023

Minna Chow

Minna Chow

Milo Chang

Milo Chang


AP Computer Science Principles ⌨️

80Β 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.

Repeat n Times

In College Board's Pseudocode, the first is a REPEAT n TIMES loop, where the n represents some number.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-lv2Xc0E5Go2A.png?alt=media&token=307374a0-b7f3-4896-8b2c-2c4aadd0b9bc
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
REPEAT n TIMES:
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)

Repeat Until (condition)

The second type of loop is a REPEAT UNTIL (condition) loop, where the loop will continue to run until a condition is met.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-ksXJia3Km0ef.png?alt=media&token=67a024f6-c2a3-4190-b8b2-f777595f3d40
In 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!

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.