Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

3.8 Iteration

3 min readjanuary 3, 2023

Minna Chow

Minna Chow

Milo Chang

Milo Chang

Minna Chow

Minna Chow

Milo Chang

Milo Chang

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 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 , 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 . 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 .

❗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!

Key Terms to Review (7)

AND Operator

: The AND operator is a logical operator that returns true only if both operands are true; otherwise, it returns false.

for... in range loop

: A for...in range loop is a type of loop that repeats a block of code for a specific number of times. It iterates over a sequence of numbers defined by the range function.

Infinite Loops

: Infinite loops are loops that continue to repeat indefinitely because the condition controlling the loop is always true or there is no condition at all.

Iterative Statements

: Iterative statements, also known as loops, are programming constructs that allow a set of instructions to be repeated multiple times based on a specified condition. They help automate repetitive tasks and make programs more efficient.

OR Operator

: The OR operator is a logical operator that returns true if at least one of its operands is true; otherwise, it returns false.

REPEAT UNTIL (condition) loop

: A REPEAT UNTIL (condition) loop is a type of loop that repeats a block of code until a certain condition becomes true. It checks the condition after executing the code block, ensuring that it runs at least once.

while loop

: A while loop is a type of conditional looping structure that repeatedly executes a block of code as long as its specified condition remains true. It checks the condition before executing the code block.

3.8 Iteration

3 min readjanuary 3, 2023

Minna Chow

Minna Chow

Milo Chang

Milo Chang

Minna Chow

Minna Chow

Milo Chang

Milo Chang

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 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 , 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 . 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 .

❗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!

Key Terms to Review (7)

AND Operator

: The AND operator is a logical operator that returns true only if both operands are true; otherwise, it returns false.

for... in range loop

: A for...in range loop is a type of loop that repeats a block of code for a specific number of times. It iterates over a sequence of numbers defined by the range function.

Infinite Loops

: Infinite loops are loops that continue to repeat indefinitely because the condition controlling the loop is always true or there is no condition at all.

Iterative Statements

: Iterative statements, also known as loops, are programming constructs that allow a set of instructions to be repeated multiple times based on a specified condition. They help automate repetitive tasks and make programs more efficient.

OR Operator

: The OR operator is a logical operator that returns true if at least one of its operands is true; otherwise, it returns false.

REPEAT UNTIL (condition) loop

: A REPEAT UNTIL (condition) loop is a type of loop that repeats a block of code until a certain condition becomes true. It checks the condition after executing the code block, ensuring that it runs at least once.

while loop

: A while loop is a type of conditional looping structure that repeatedly executes a block of code as long as its specified condition remains true. It checks the condition before executing the code block.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.