While Loops

Below is the now familiar count_up function written using a while loop. Unlike the for loop, while does not have a built-in index variable (but we can easily define our own using the variable i below).


def count_up(num):
    i = 1
    while i <= num:
        print(i)
        i += 1
                    

The while loop is analogous to the repeat until loop in Snap! However, in Python the loop ends when the condition becomes False (so you can think of a while loop as "repeat the code in the loop while True"). This is the opposite of Snap!'s repeat until loop, which waits for its condition to become True.