Python Tutorial 10 --- "While Loops In Python"
Python Tutorial 10 ---
"While Loops In Python"
This PROGRAM or TUTORIAL about Python's "While Loops In Python"
DOC
Let us understand the concept of the while loop and why do we use it in areas where the number of iterations are not defined or we do not have any idea how many iterations would take place with the help of an example. Suppose that we have created an application for an ATM from where a customer can only withdraw money up to 5000 rupees. Now our condition in the while loop would be to iterate unless the input is between 1 to 5000. So, the condition will be true unless the user inputs a number between 1 to 5000, so the loop will iterate depending upon the time the user submits the wrong input,
For a While loop to run endlessly we can pass true or 1 as a condition. 1 is also used in place of writing true as a whole. So, in this case, the condition will never become false so the program will run endlessly. But these kinds of programs do not output anything because they could never complete their execution.
CODE
#While Loop in Python
"""A while loop in python runs a bunch of code or statements again and again
until the given condition is true when the condition becomes false,
the loop terminates its repetition."""
#To terminate an infinite loop, you can press Ctrl+C on your system.
i = 0
while i<=30:
print(i)
i=i+1
"""Output---
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
Comments
Post a Comment