-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17_While Loops.py
94 lines (73 loc) · 1.17 KB
/
17_While Loops.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
1. While Loop -
"A while loop in python runs a bunch of code or statements again and again until the given condition is true. When the condition become false, the loop terminates its repetition."
2. Syntax for 'while' loop -
while codition_is_true:
Code inside the loop body
3. Usecase for 'while' loop -
a) A 'while' statement runs until the condition becomes false.
b) A 'while' statement is used for areas whereas the number of iterations are unknown.
'''
'''
#Example -1
i = 1
while (i<=20):
print(i)
i = i+1
#O/P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'''
#Example 2 - with break statement
'''
i = 1
while i<10:
print(i)
if i == 6:
break
i=i+1
#The break statement stops the loop even if the while conndition is true. So, here the loop is stopped at number 6.
#O/P -
1
2
3
4
5
6
'''
#Example 3 - with continue statement
i = 0
while i<10:
i=i+1
if i == 6:
continue
print(i)
#The continue statement stops the current iteration and continue with the next. So, here the number 6 is skipped and the next iteration is continued.
#O/P -
1
2
3
4
5
7
8
9
10