forked from adilhsnk/Python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
While loops
34 lines (23 loc) · 1.04 KB
/
While loops
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
31
32
33
34
#!/bin/python3
def nl():
print('\n')
nl()
#While loops - executes as long as True. It's the same as if I use 192.168.1.x where i want x to be repeated from 1 to 255.
i = 1 #Initially, the value for i equals to 1.
while i < 10: # until i is less than 10, repeat the loop. once it reaches 10, stop (it will print upto 9)
print(i)
i += 1 #print i and increase i with 1 each time until it reach 10.
#Infinite loop
count = 0
while count < 5:
print("This loop will never stop")
print("This is infinite loop")
#count = count + 1
#OR count += 1
# They will execute as long as True
# A while loop performs an operation while a certain condition is true. You could use a while loop to:
# Check for another line in a file.
# Check if a flag has been set.
# Check if a user has finished entering values.
# Check if something else has changed to indicate that the code can stop performing the operation.
lets say the user is entering names in the list on a computer and is not finished yet. so the code may execute again and again until the user says "Finish"