Python While Loop with example

Python While Loop with example

Python while loop. In this post, you will learn everything about python while loop in detail with examples.

What is while loop in Python?

The while loop in Python, which is used to iterate the block of statement as long as the test condition is true.

Do you know the meaning of Iteration?

In any programming language, to execute a block of code repeatedly. A programming structure that implements iteration is called a loop.

Syntax of while Loop in Python

while test_condition:
    Body of while

How to work while loop?

1. First, the given condition is checked, if the condition returns false, the loop is terminated and the control jumps to the next statement in the program after the loop.
2. If the condition returns true, the set of statements inside loop are executed and then the control jumps to the beginning of the loop for next iteration.

Note:- If you see the flowchart of the below given Python while loop, you will understand how the wheel loop works.

Flowchart of while Loop

Example: Python while Loop

n = 5

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)

Output

The sum is 15

Explanation of above program

Our program will continue until the counter is equal to or less than 5. When the counter becomes larger than 5, the test condition will fall away. And we will get the value of the total number as the output

Python while loop with else

While the loop is with another statement, the loop is also with another.

When the test condition is false in loop evaluates then the second ELSE statement part is executed.

Example: Python while Loop with else

counter = 0

while counter < 3:
    print("While loop")
    counter = counter + 1
else:
    print("The while loop finished")

Output

While loop
While loop
While loop
The while loop finished

Explanation of above program

Here we have conditioned the loop to iterate 3 times. When the loop will iterate 3 times, then the part with the else statement will be executed. You can also see the upper output.

And you can also run this program in your python interpreter and run it.

python while true loop example

If the loop condition will never be false then this loop will run indefinitely. And you can say that if the condition is always true, it will never stop.

while True:
   print("hello")

In the above program, in this while loop, we have passed the test condition instead of true. So this will never stop.

Nested while loop in Python

When a while loop is present inside another while loop then it is called nested while loop.

Example: Nested while loop in Python

i = 1
j = 5
while i < 4:
    while j < 8:
        print(i, ",", j)
        j = j + 1
        i = i + 1

Output

1 , 5
2 , 6
3 , 7

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *