Tech With Tim Logo
Go back

While Loops

While Loops

A while loop is very similar to a for loop. It simply continues to do something while a given condition is true. We typically use while loops when we are unsure about how many times we may possibly be looping (or repeating something). The syntax looks something like this:

while condition == True:
    print("I will run until the conditon is False")

Breaking a Loop

Sometimes while we are looping we may encounter a situation where we want to exit the loop. It may be the case that the condition keeping the loop running is still True but we would still like to "break" the loop. We can do this using the "break" keyword. Whenever we encounter the word "break" within a loop (for or while) we will immediately exit that loop.

while True:
    inp = input("Type a number: ")
    if inp == "5":
        break  # when 5 is typed we will stop asking the user to type a number
    print("Try again...")  # Try again will not be printed if 5 is typed

Example

Here is a common example of when we would use a while loop:

# When we want to keep asking the user for input until we get a certain value
run = False

while run:
    answer = input("What is 5 + 7")
    
    if answer == "12":
        print("Correct!")
        run = False
    else:
        print("Try Again...")
Design & Development by Ibezio Logo