...

/

The while Loop

The while Loop

Learn about the essential features and usage of 'while' loops for effective iteration in programming.

The while loop keeps iterating over a certain set of operations as long as a certain condition holds True. It operates using the following logic:

While this condition is true, keep the loop running.

Structure

In a for loop, the number of iterations is fixed since we know the size of the sequence. On the other hand, a while loop is not always restricted to a fixed range. Its execution is based solely on the condition associated with it.

Press + to interact

The while loop in action

Here's a simple example of a while loop in Python that prints the numbers from 1 to 5. Note that the while statement also ends in a colon, similar to the ...

Access this course and 1400+ top-rated courses and projects.