-
Notifications
You must be signed in to change notification settings - Fork 2
/
04-the_while_loop.py
68 lines (51 loc) · 1.22 KB
/
04-the_while_loop.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
# "while" loop will do something while an expression evaluation equals to true.
i = 0
while i < 5:
print(i)
i += 1
# Behaviour of "do-while" concept can be emulated as well. This construct ensures that the code block inside "while" loop executes at least once.
i = 5
while True:
print(i)
if i >= 5:
break
print("Something.")
# "break" statement can break out of the "while" loop.
min_length = 2
while True:
name = input("Please enter your name: ")
if len(name) >= min_length and name.isprintable() and name.isalpha():
break
print("Hello, {0}.".format(name))
# "continue" statement allows for "while" loop to continue executing even if the condition in "if" statement is not met.
a = 0
while a < 10:
a += 1
if a % 2 == 0:
continue
print(a)
# "else" can be used with "while" loop if the loop did not end with a "break".
# Classic approach.
l = [1, 2, 3]
val = 10
found = False
idx = 0
while idx < len(l):
if l[idx] == val:
found = True
break
idx += 1
if not found:
l.append(val)
print(l)
# "else" approach.
l = [1, 2, 3]
val = 10
idx = 0
while idx < len(l):
if l[idx] == val:
break
idx += 1
else:
l.append(val)
print(l)