Latest posts

10/recent/ticker-posts

While loop in Python Programming | While loop example | Python while loop

In a recent post on Python programming, we have seen For Loop statement of Python programming. In this blog post, we will explore the while loop in Python programming in depth. We will cover what the while loop is, how it works, and provide examples of how it can be used. By the end of this blog post, you should have a solid understanding of how to use the while loop in Python programming.

What the while loop is ?

A while loop is a type of loop that executes a block of code as long as a certain condition is true.
Simply, it is a type of looping statement, that iterates a block of code, until the given condition becomes false. This loop is total works on conditions.
When the user gives any certain condition which will never become false any single time, then the loop goes in an infinite loop.

Let's see the syntax for a better understanding :
while condition :
    # Code to be execute

The condition is a Boolean expression evaluated before each iteration of the loop. If the condition is true, the code block is executed. After the code block is executed, the condition is evaluated again, and if it is still true, the code block is executed again. This process continues until the condition is false, at which point the loop terminates and control passes to the next line of code.

Simply, First, it checks the condition, when the condition is true, it executes the first iteration of a code block. After that it again checks the condition whether it is true or false, if the condition is true, then it will again iterate the block code. This cycle runs continuously until the condition becomes false.

It is important to note that the condition in a while loop must eventually become false in order for the loop to terminate. If the condition never becomes false, the loop will continue to execute indefinitely, which can cause the program to hang or crash.

How does a while loop works ?

To understand how a while loop works, let's look at an example. Suppose we want to print the numbers 1 to 10 using a while loop.
We can do this using the following code :
i = 1
while i <= 10:
    print(i)
    i += 1



In this example, we initialize the variable i to 1 and then enter the while loop. The condition i <= 10 is true when i is less than or equal to 10, so the code block is executed. The code block consists of a single line that prints the value of i using the print() function and then increments the value of i by 1 using the += operator. After the code block is executed, the condition i <= 10 is evaluated again. If the condition is still true, the code block is executed again, and the value of i is printed again. This process continues until the condition i <= 10 is false, at which point the loop terminates.

In this example, the while loop terminates when the value of i becomes 11 because the condition i <= 10 is no longer true.

This example demonstrates how a while loop works. The loop executes the code block repeatedly as long as the condition is true and terminates when the condition becomes false.

Use of while loop in Python Programming :

Now that we understand what a while loop is and how it works, let's look at some examples of how it can be used in Python programming.

Fibonacci Series :
n = int(input("Enter a Number : "))
sum = 0
i = 0

print(f"Fibonacci Series from 1 to {n} is : ")
while i <= n :
    sum += i
    i += 1
    print(sum)




This Python code takes an integer input from the user and generates the Fibonacci series up to that number. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.

In the first line of code, the input function is used to prompt the user to enter a number, which is then converted to an integer using the int() function and assigned to the variable n. The sum variable is initialized to 0 in the second line, and i is initialized to 0 in the third line. The print statement in the fourth line prints a message to the user stating that the Fibonacci series will be generated up to the entered number.

The while loop begins in the fifth line with the condition i <= n, which checks if i is less than or equal to n. As long as this condition is true, the loop will continue to execute. Inside the loop, the sum variable is updated by adding the value of i to it using the += operator in the sixth line. The value of i is then incremented by 1 in the seventh line. The print statement in the eighth line prints the current value of the sum variable, which generates the Fibonacci series. This process continues until i is greater than n, at which point the loop terminates.

So here we have seen, how a while loop works in Python.

We hope it will help you to learn Python programming more.
Thank you!



Also Visit


Post a Comment

0 Comments