In this article, you will learn how to Construct While Loops In Python. Before we get started, if you want Conditional Statements in Python, please go through the following article: Conditional Statements in Python.

Loops are an essential part of any programming language. As other loops, while loops in python allow programmers to set certain portions of their code to repeat through a number of loops which are referred to as iterations.

While Loop In Python

A while statement iterates a block of code until the controlling expression evaluates to True. While loop favors indefinite iteration, which means we don’t specify how many times the loop will run in advance. In Python, a basic while loop looks like this:

The condition we provide to the while statement is the controlling expression, our loop will run until the controlling statement is no longer true. Let’s take an example;

Here, Output will be:

In the above example, we first assigned the value 1 to the variable x, then we constructed a while loop which will run until the value of x is less than or equal to 3. Inside the loop first, we are printing the current value of x  then incrementing the value of x by 1.

In the fourth iteration, the while condition is no longer true as the value of x is now 4, therefore the while loop terminates. A quick note – You should be really careful while constructing a while loop because if you assign a condition that will never turn into False, the loop will run forever resulting in the infinite loop.

The else Clause In While Loop

Python provides a unique else clause to a while loop to add statements after the loop termination. This can be better understood by the following example,

Output:

We can see the else clause executes after the loop termination. But wait! We can get a similar output even by just adding a print statement below, why use the else clause then?

Because else clause we will only execute when the loop gets terminated by itself, not by any interrupt like the break keyword. Python comes with two inbuilt keywords to interrupt loop iteration, break and continue.

Break Keyword In While loop

The break keyword immediately terminates the while loop. Let’s add a break statement to our existing code,

Output:

We added a break statement for condition x holding the value 2, which will immediately terminate the while loop from that point, thus 3 was not printed. Neither the else clause was executed because the while loop was interrupted by the break keyword.

Continue keyword in While Loop

The continue keyword terminates the current iteration, not the entire loop when the compiler encounters the continue it goes straight back to the top of the while loop.

Output:

2 isn’t printed in the output because we introduced the continue keyword for this particular condition, when x holds the value 2 in it the Python interpreter hits the continue keyword and goes back to the top of the while instead of printing it.

How To Construct While Loops In Python

The article was published on September 27, 2020 @ 9:24 AM

Leave a Comment