Loops


While Loops

A while loop is a block of code that will continue ‘looping’ (repeating) until its boolean condition becomes false.

Syntax

while [boolean expression "test"]:
    # useful code that executes as long as the boolean 
    # expression is true (repeat block)

    # Manipulation of boolean condition (i.e. changing boolean 
    # expression "test" from True to False by manipulating 
    # counter variable, i.e. i += 1)

After the last statement in the repeat block completes, the computer will jump backwards to the test and start anew. If the test evaluates to True, the computer will move through the repeat block again. If the test evaluates to False, the computer will ‘exit’ the loop, skipping over the repeat block.

While Loop Checklist

  1. Boolean condition
    1. Located after ‘while’ and before ‘:’
    2. Can use any boolean operator (==, !=, >, <. >=. <=)
    3. Can also use a boolean variable (a_condition)
  2. Code inside the while block (repeat block) is useful
  3. A ‘counter’ variable or boolean expression that will eventually become False and exit the loop – this should be placed inside the body of the while loop.

Examples of Counters

Numerical counter:

while i < 10:
    # looping code
    i += 1

Common ways to increment a numerical counter include:

i += 1  # the same as i = i + 1
i -= 1  # the same as i = i - 1

The counter variable can also be incremented by numerical amounts other than 1.

i = i + 3
i = i - 5

Boolean expression:

weekend: bool = False
day_of_week: int = 1

while weekend == false:
    # looping code
    if day_of_week == 6 or day_of_week == 7:
        weekend = True
        print("Yay, it's the weekend!")
    day_of_week += 1

Something is Wrong!

If your loop just won’t end, you probably have implemented an infinite loop. Make sure that your condition can eventually become false! Example:

# incorrect:

i: int = 0
while i < 110:
    print("I love COMP110!")
print("Done")

This is incorrect because the counter ‘i’ is never changed and thus will never be >= 110. Therefore, the while loop will INFINITELY execute and the string “Done” will never print.

# correct:

i: int = 0
while i < 110:
    print("I love COMP110!)
    i += 1
print("Done")

Now, the while loop will execute 110 times, printing “I love COMP110 each time. The string”Done" will be printed once i = 110.

Nested While Loops!

We can nest while loops, meaning we can place one while loop inside of another! Check out this example:

sound: str = ""
i: int = 1  
while i < 20: 
    count: int = 0 # each time the outer loop runs, count is reassigned to 0
    while count < 4:
        i *= 2  # increase the value in i, the counter variable for the outer loop
        count += 1  # increment count, the counter variable for the inner loop
        sound += "e"
    sound += "-iii-"
sound += "oh"

print(sound)  # will print the string "eeee-iii-eeee-iii-oh"

These nested loops build up the string ‘sound’. The inner loop starts over and runs through again each time we reenter the outer loop.

For-in Loops

for-in loops are similar to while loops, but instead of looping through a statement until a boolean condition changes, a for-in loop iterates over a collection of items.

The Syntax:

for [identifier] in [sequence]:
    # useful code that processes each identifier(item) in a sequence(collection). (repeat block)

identifier is a variable name you choose for each item in the repeat block.

How Does this Work?

The assignment of the identifier to each item in the sequence is handled for you, and so is the progression from one item to the next. (Thank you, computers!)

The loop ends after the repeat block evaluates the last item.

for-in loops are great for when you want to process every item in a collection. Collections can include Lists, Dictionaries, Sets, and even Strings! Anything iterable can be processed by a for-in loop.

One advantage of for-in loops are since iteration is taken care of by the computer, we don’t have to worry about forgetting to increment/decrement our counter variable (goodbye infinite loops!).

Converting a While Loop

Any while loop can be turned into an equivalent for-in loop. Here’s an example of printing out elements of a List:

from typing import List

i: int = 0
my_list: List[int] = [1, 2, 3, 4, 5, 6]

while i < len(my_list):
    print(my_list[i])
    i += 1

# can be rewritten as:

for item in my_list:
    print(item)

The output for both of these loops will be exactly the same: 1, 2, 3, 4, 5, 6. These two types of loops are completely interchangeable; however, the syntax of a for-in loop often proves to be preferable as you get more comfortable with the process.

Which Loop to Use?

Although for-in loops and while loops are interchangeable, there are some instances where you want to use one over the other.

While loops provide more control to the author because you must know how many times the loop should run – either by a specific numerical amount using a counter varible, or by telling the loop when to end based on a boolean condition.

for-in loops are fantastic for-in looping through an entire collection of data in a deceptively simple way.

Nested For Loops!

We can nest for-in loops, too!

from typing import List

list_1: List[str] = ["spooky", "scary"]
list_2: List[str] = ["skeletons", "shivers"]

for word in list_1:
    for item in list_2:
        print(word, item)

The nested loops print out combinations of the two lists. The output of this will be ‘spooky skeletons, spooky shivers, scary skeletons, scary shivers’.

Range Objects in Loops

You can also loop through a defined range using Python’s range() function.

Check out this page for more information about this function!

While the syntax looks a bit different for using range within with loops and for-in loops, both can use the range() function in their implementation!

i: int = 0
while i in range(5):
    print(i + ", ")
    i += 1

for i in range(5):
    print(i + ", ")

Both loops’ outputs are ‘0 1 2 3 4’. This is another way to control the amount of times a loop repeats!

Contributor(s): Claire Helms