1 of 21

While Loops Review

Thursday, September 12th, 2024 | CSCI100: Intro to Computer Science

2 of 21

What are while loops and why do we use them?

  • Remember that while loops allow us to run a piece of code repeatedly as long as a condition is met
  • Some ways that this shows up in our day-to-day are:
    • Driving a car (while you are pressing the gas pedal, the car accelerates)
    • Playing a game of UNO (while everyone has cards, we keep playing the game)
    • Participating in spelling bee (while you continue to spell words correctly, you can still compete)

3 of 21

  • If we were to visualize while loops as a decision tree: it would look something like this:

4 of 21

While loop syntax

  • Generally while loops are written as follows:

while boolean_expression:

# statements to run

# if boolean expression evaluates to True

  • Python uses indentation to identify code that is and is not inside a while loop. This is known as scope

i = 10

while i < 15:

print(i, “is less than 15”)

i += 1

print(“I’m not in the while loop”)

5 of 21

Code Reading Exercise

6 of 21

Code Reading Exercise

num1 is greater than 0 so go into the loop

7 of 21

Code Reading Exercise

num2 equals num2 * 10 (0) + num1 % 10 (3)

So num2 = 3

8 of 21

Code Reading Exercise

Divide the current value of num1 by 10 and update num1 to that quotient. Num1 is now 524

9 of 21

Code Reading Exercise

Num1 (524) is greater than 0 so go into the loop

10 of 21

Code Reading Exercise

Num2 (3) equals num2 * 10 (30) + num1 % 10 (4)

So num2 = 34

11 of 21

Code Reading Exercise

Divide the current value of num1 by 10 and update num1 to that quotient. Num1 is now 52

12 of 21

Code Reading Exercise

Num1 (52) is greater than 0 so go into the loop

13 of 21

Code Reading Exercise

Num2 (34) equals num2 * 10 (340) + num1 % 10 (2)

So num2 = 342

14 of 21

Code Reading Exercise

Divide the current value of num1 by 10 and update num1 to that quotient. Num1 is now 5

15 of 21

Code Reading Exercise

Num1 (5) is greater than 0 so go into the loop

16 of 21

Code Reading Exercise

Num2 (342) equals num2 * 10 (3450) + num1 % 10 (5)

So num2 = 3425

17 of 21

Code Reading Exercise

Divide the current value of num1 by 10 and update num1 to that quotient. Num1 is now 0

18 of 21

Code Reading Exercise

Num1 (0) is not greater than 0 so don’t go into the loop

19 of 21

Code Reading Exercise

Print the values of num 1 and num2

num1: 0

num2: 3425

20 of 21

Code Writing Exercise

    • Write a program to ask the user to enter their password (the password is “M!llIon@!r1”). They get 3 attempts to guess their password, and if they don’t get it in 3 tries, tell them to try again later

21 of 21

While loop Tips

    • Remember that while loops run as long as a condition is true, so make sure that the boolean expression in your while statement reflects that
    • Make sure that you prevent infinite loops by changing the value of the variable in your while statement
    • If you do get an infinite loop, you can end the program by pressing CTRL+C