What is While Loop in Python?

       While a loop is also called a conditional loop, it means it is also used for iterations, but its iterations depend on a specific condition.

A while loop in Python is a way to repeat a block of code as long as a specific condition is true. It keeps running until the condition becomes false. A while loop runs the code inside it over and over until the condition you set is no longer true.

Syntex

while condation:
    # block of code

Where while is a keyword and condition is a specific condition that can be written by use with the help of any conditional operator:

Here are the common conditional operators:

  • == : equal to
  • != : not equal to
  • > : greater than
  • < : less than
  • >= : greater than or equal to
  • <= : less than or equal to

Let's take a very simple example with while loop:

while 10 == 10:
    print("theagleye")

  In this example, the given condition is always getting True because 10 is always equal to 10, so it runs infinite times till the system does not crash.

Output:

theagleye
theagleye
theagleye
theagleye
theagleye
theagleye
theagleye
theagleye
theagleye
theagleye
theagleye
....
...
.

So how to handle while loop infinite iterations this now?
For this, you have to apply some logic in your code to make the condition false, or this is very easy just write for while you wish to run this code.

Problem: Write a Python program to print from 1 to 10 using a while loop.

Solution:

num = 0

while num < 10:
    num = num + 1
    print(num)

  The code uses a while loop to count from 1 to 10. Each time the loop runs, it increases num by 1 and prints the new value until num reaches 10.

Output:

1
2
3
4
5
6
7
8
9
10

=== Code Execution Successful ===

 

Problem : Problem: Write a Python program to print a table of 7 using a while loop.

 Solution:

x = 1

while x < 11:
    print("7 x ", x, "=", 7*x)
    x = x + 1

Output:

7 x  1 = 7
7 x  2 = 14
7 x  3 = 21
7 x  4 = 28
7 x  5 = 35
7 x  6 = 42
7 x  7 = 49
7 x  8 = 56
7 x  9 = 63
7 x  10 = 70

=== Code Execution Successful ===

 

Little advance while loop example

Problem : You are simulating an ATM system. The ATM allows a user to withdraw cash as long as their balance is greater than zero. The user can withdraw a specific amount or quit the process. The program should keep asking the user for the withdrawal amount until they enter a negative amount, which will indicate that they want to exit.

  Solution:

# Initial balance in the ATM
balance = 1000  # User's starting balance

print("Welcome to the ATM!")
print("Your current balance is $", balance)

while True:
    withdrawal = float(input("Enter amount to withdraw (or a negative number to exit): "))
    
    # Check if the user wants to exit
    if withdrawal < 0:
        print("Thank you for using the ATM. Goodbye!")
        break

    # Check if the withdrawal amount is valid
    if withdrawal > balance:
        print("Insufficient funds. Your current balance is $", balance)
    else:
        balance -= withdrawal  # Deduct the withdrawal amount from balance
        print("You have withdrawn $", withdrawal)
        print("Your new balance is $", balance)

Output:

Welcome to the ATM!
Your current balance is $ 1000
Enter amount to withdraw (or a negative number to exit): 320
You have withdrawn $ 320.0
Your new balance is $ 680.0
Enter amount to withdraw (or a negative number to exit): 10
You have withdrawn $ 10.0
Your new balance is $ 670.0
Enter amount to withdraw (or a negative number to exit): 200
You have withdrawn $ 200.0
Your new balance is $ 470.0
Enter amount to withdraw (or a negative number to exit): 400
You have withdrawn $ 400.0
Your new balance is $ 70.0
Enter amount to withdraw (or a negative number to exit): 

Leave a comment

You must be logged in to post a comment.

0 Comments