While Loop in Python with Examples - Scaler Topics (original) (raw)

Overview

Python While Loop is a programming construct used to repeatedly execute a set of statements until a condition is met. When the condition in the program becomes false, the line immediately after the loop is performed. While loop is classified as an indefinite iteration.

What is While Loop in Python?

While loop statement in Python is used to execute statement(s) repeatedly. The number of times the while loop is executed is not known in advance, so the while loop is called an Indefinite Iteration statement. It will keep on repeatedly executing as long as a while condition is true, and it will stop repeating only if the condition becomes false.

Let us say you are asked to print your name in Python 100 times. The most naive approach would be to type/copy-paste the statement print(“Name”) 100 times and execute. But that is such a redundant task with 100 lines of Python code.

The best possible approach will be using the while loop statement in Python. And here’s how it works: Write a while loop. Place the print(“Name”) inside the while loop. Write a condition so that it fails after executing 100 times, and voila.

This code is at most 4-5 statements, unlike the naive approach, which took 100 statements. So, in other words, a while loop makes our life easy.

Syntax of While Loop in Python

Below is an explanation of the components in syntax:

Working of while:

We shall explore the working of while loop more clearly with an example in the next sections of the article.

FlowChart of While Loop in Python

flowchart of while loop in python

Example of Python While Loop

#1 Write a program to print a name ten times in Python using the while loop

Program:

Output:

Explanation:

While Loop with else

While statement in python also supports else statement in association with it. The else statement is optional. Below points illustrate the working of while else:

Syntax:

The body of else can be a single statement or a block of uniformly indented statements. The else is executed only once after the while loop condition returns false, and the loop breaks.

Example:

Output:

Explanation:

Single Statement While Loop in Python

If there is only one statement in the body of the while loop, the statement can be placed in the same line as the while header statement. This variant is supported in python. The below syntax makes this clear.

This is only allowed for a single statement if there are multiple statements, all of them should be written inside the body of the while loop, and that too uniformly indented.

Example:

Output:

Explanation:

The Infinite While Loop in Python

The Infinite while loop is a loop that never stops its repeated execution because the expression ( condition ) defined in the while header will never return False. The example we saw in the above section is one such infinite while loop.

There can be many use cases of Infinite while loop in programming. A simple instance is a system/program that keeps on running without stopping, like a server program. So that the client program can connect to the server program anytime as long as the server program keeps running.

Let us look at an example Infinite while loop program

Output:

Explanation:

While Loop Control Statements in Python

Loop control statements/ keywords in python written inside a while loop affect the flow of execution of the while loop. They alter the normal execution of a loop. Below are the loop control statements that are supported in python:

Continue Statement:

Continue statement written inside the body of while loop returns the flow of control back to the expression in the header, skipping the execution of the rest of the statements inside the body.

Example:

Output:

Explanation:

Break Statement:

Break statement inside the body of a while loop will stop the repeated execution and bring the control out of the while loop. It is like an abrupt force stop of a while loop. If it is a while..else statement, then the else part is also skipped when a break statement is executed. Control jumps directly to the statement below the while..else statements.

Example:

Output:

Explanation:

Pass Statement:

A pass statement can be seen as a dummy/empty statement. It is used to write empty loops, empty blocks, empty functions, classes, etc. It doesn’t really affect the flow of control. It is used to keep the flow seamless.

Example:

Output:

Explanation:

Learn More:

Check out this article also and learn about which module in python supports regular expressions by clicking here.

FAQs

Q: What is the while loop in Python?

A: The while loop in Python is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition is true. It provides a way to create iterative processes in your program.

Q: What is the difference between a while loop and a for loop in Python?

A: The main difference between a while loop and a for loop is the way they control the flow of execution. A while loop repeatedly executes a block of code as long as a condition is true, whereas a for loop iterates over a sequence (such as a list or a range) and executes the block of code for each element in the sequence.

Q: How to avoid infinite loops with while loops?

A: To avoid infinite loops while loops, ensure that the condition within the loop will eventually become False. Otherwise, the loop will continue indefinitely. Make sure to include code within the loop that modifies the variables involved in the condition, allowing the condition to become False at some point.

Conclusion

We have reached the end of the article. The examples we took in this article are very basic to give you a clear, in-depth understanding of the while loop in Python. Once you get the hang of the while loop and its variants, you can try to explore the below classic while loop programs in Python.