Python

Python While Loop

When considering a python while loop we provide condition before, that is in first step then we add increment or decrement later. Which is different when compared to for loop implementation.

Python while loop is implemented to run a piece of code until a certain condition is satisfied or requirement is reached i.e, a while loop is used to repeatedly execute a block of code as long as a condition is true.

The code block under the while loop will be executed repeatedly as long as the condition is true. When the condition becomes false, the program will exit the loop and continue with the next line of code.

It’s important to ensure that the condition in a while loop will eventually become false, otherwise the loop will continue indefinitely and the program will hang. To avoid this, you can use a break statement to exit the loop if a certain condition is met, or a counter variable that limits the number of times the loop can execute.

Now let’s start implementing a sample while loop to understand the concept.

x = 10

while x < 10;
    print("Welcome")

When you run the above code then you will never observe any output because x is never less than 10.

And when you slight change the code by making x = 1 instead of 10.

x = 1

while x < 10;
    print("Welcome")

Output :

Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
.
.
.

Python While Loop Video Tutorial :

Go through the below tutorial on python while loop for detailed implementation.

The output is never ending because we have not provided any increment or decrement conditions.

Now try to add them and run the code.

x = 1

while x < 10;
    print("Welcome")
    x+=1

Output :

Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome

Now welcome is printed 9 times as condition is satisfied at last step i..e, x > 10 so 10th time its not printed.

Indentation is the key step because without proper indentation code is not recognized and errors may arise.

Nested While Loop :

We can add nested while loops inside a while loop so that we can have multiple loops going to process data.

A nested while loop in Python is a loop that is placed inside another loop. The nested loop will execute its code block multiple times for each iteration of the outer loop.

It’s very simple to implement if you understand the concept.

x = 1
y = 5
while x < 10;
    print("Welcome")
    while y < 10; 
       print("Python");
       y+=1
    x+=1

Output :

You can clearly observe that initially Welcome is printed later Python is printed for 5 times and followed by Welcome again if you observe the code it says the same.

Welcome
Python
Python
Python
Python
Python
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome

We can observe python is been printed in output.

I have clearly explained the flow in video tutorial attached with debug process.

break :

Using break condition in while loop is super easy again as the word states it breaks the flow and returns control.

The break statement can be used inside a while loop to exit the loop prematurely, even if the loop condition has not yet become false. The break statement immediately terminates the innermost loop that it is contained within.

x = 1

while x < 10;
    print("Welcome")
    if x == 5
       break;
    x+=1

Output :

Welcome
Welcome
Welcome
Welcome
Welcome
Welcome

The output is terminated at x == 5 so exited.

If you have any query’s in this tutorial on Python While Loop do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..

Abhishek

Share
Published by
Abhishek

Recent Posts

5G and 6G Technology Advancements: A Promising Future of Connectivity

5G and 6G Technology AdvancementsWhat is 5G, and How Has It Transformed Connectivity?1. Understanding 5G…

2 months ago

HTML: The Evolution and Power of Unleashed Web Language and Modern too

IntroductionInitial StagesEvolutionChallenging Other LanguagesCurrent TrendsAI and HTMLConclusion Introduction HTML, or HyperText Markup Language, is the…

5 months ago

Increase in 80C in Budget 2024 ?

Increase in 80CDemands and Discussions: Increase in 80C Section 80C of the Income Tax Act…

5 months ago

ChatGPT 4o Unleashing the Power of GPT A Comprehensive Guide

IntroductionWhat is ChatGPT-4?Key Features of ChatGPT-4Enhanced Natural Language UnderstandingImproved Response GenerationVersatilityApplications of ChatGPT-4Customer SupportContent CreationEducational…

8 months ago

APJ Abdul Kalam Biography: A Tribute to India’s Powerful Missile Man

APJ Abdul Kalam Biography :Childhood :Academics :Professional Career :Achievements : APJ Abdul Kalam Biography :…

10 months ago

Srinivasa Ramanujan Biography: Exploring the Genius

We value your feedback! Please share your thoughts on this blog on Srinivasa Ramanujan Biography…

11 months ago

This website uses cookies.