Search⌘ K
AI Features

The while Loop

Explore how to use the while loop in Python to repeat code until a condition is met. Understand how to control loop execution with break and continue statements, avoid infinite loops, and apply increment shortcuts for effective looping.

We'll cover the following...

The while loop is also used to repeat sections of code, but instead of looping n number of times, it will only loop until a specific condition is met. Let’s look at a very simple example:

Python 3.5
i = 0
while i < 10:
print(i)
i = i + 1

The while loop is kind of like a conditional statement. Here’s what this code means: while the variable i is less than ten, print it out. Then at the end, we increase i’s value by one. ...