while Loop
☞ It is used when you want to execute a block of code as long as a condition is true.
☞ To make it work, three statements are needed : initialization, condition and flow.
☞ You can use while loop alone or with else also.
☞ If you are using while-else, then else will execute when test_condition will become false.
Syntax :
while <test_condition>:
#statements of while
else: #optional
#statements of else
Example 1 :
#To print a name 5 times
i = 1 #initialization
while i<=5: #condition
print("inspirewebsoft")
i = i + 1 #flow
Output :
inspirewebsoft
inspirewebsoft
inspirewebsoft
inspirewebsoft
inspirewebsoft
Explanation :
- i is initialized to 1. This is the counter variable that will keep track of how many times the name "inspirewebsoft" has been printed.
- The while loop runs as long as the condition i <= 5 is true.
- Inside the loop, the string "inspirewebsoft" is printed using the print() function.
- After printing the name, the value of i is incremented by 1 (i = i + 1), ensuring that the loop moves towards its termination condition.
- This process continues until the value of i exceeds 5, at which point the condition i <= 5 becomes false, and the loop terminates.
The result is that the name "inspirewebsoft" is printed exactly 5 times, each time on a new line.
Example 2 :
#To print a name 5 times and demonstrate use of else
i = 1
while i<=5:
print("inspirewebsoft")
i = i + 1
else:
print("Thank you!!!")
Output :
inspirewebsoft
inspirewebsoft
inspirewebsoft
inspirewebsoft
inspirewebsoft
Thank you!!!
Explanation :
- Similar to the previous example, i is initialized to 1 to serve as the counter variable.
- The while loop runs as long as the condition i <= 5 is true.
- Inside the loop, the string "inspirewebsoft" is printed using the print() function.
- After printing the name, the value of i is incremented by 1 (i = i + 1).
- This process continues until the value of i exceeds 5, at which point the condition i <= 5 becomes false, and the loop terminates.
- When the loop terminates normally (i.e., without encountering a break statement), the else block is executed, printing the message "Thank you!!!".
So, after printing "inspirewebsoft" five times, the loop exits, and then "Thank you!!!" is printed once.
☞Infinite Loop :
- Loop becomes infinite if a test_condition never becomes false.
Example :
#To illustrate infinite loop
i = 1
while i<=5: #condition resulting in an infinite loop
print("inspirewebsoft")
Output :
inspirewebsoft
inspirewebsoft
inspirewebsoft
…..
….. and so on
Explanation :
- The code initializes a variable i with the value 1.
- It enters a while loop with the condition i <= 5, which means the loop will continue executing as long as the value of i is less than or equal to 5.
- Inside the loop, the string "inspirewebsoft" is printed repeatedly.
- However, there's no increment or change to the value of i within the loop. As a result, i will remain 1, and the condition i <= 5 will always be true.
- Because the value of i never changes, the loop will continue indefinitely, resulting in an infinite loop.
- The program will keep printing "inspirewebsoft" endlessly until it is interrupted manually or stopped by some external force (e.g., by force-quitting the program or shutting down the computer).
To fix this and avoid an infinite loop, you need to ensure that the loop condition eventually becomes false by modifying the value of i inside the loop, so it can eventually exceed 5 and terminate the loop.