Professional Documents
Culture Documents
#3. While Loop
#3. While Loop
#3. While Loop
While loop
② while
③ Practice problem
⑥ break
⑤ continue
② *.
✗ do while
Keyword wh FEE
emdiʳn:¥e
unite
code
code
code
code <
Write to
print Mysia
a
program
five times .
I -26
i:-|
while 1%-5 : MySirG
)
"
Mysia
"
print ( Mysira
i -1=1 My Sing
My Sira
My SMG
Write to
print first to
a
program
natural numbers
45 6 89 ' °
.
, 2 3 7
i =\
while
I ⇐ 10 :
I = I 23 . _ . .
10
)
'
end =
'
i
print ( ,
[ c- =L
Write a
program
to
print first to
natural order
numbers in reverse
& i
0
0 10 9 8 76 5 4 3 21
9
8 t.TT#TTTTTT-T-
7
6
i ,
✓ i → 10 i > =
5
4
10 to
9
i>
9
§
3 8 8 '
✓[ > O
¥
2 7
I G-
✓[
l
True
◦ s → to
a 9 True
=L 3
"
→ a E &
0
,
0
true
false i
I print G- it
10
I
2 9
3 8
"
:
I
I 0
Write to
program
a
print first n
2 4 6 8 -
- - - n
n = /°
2 4 6 8 10 12 14 16 18 20
l=24Gi<=2*n i -1=2
i⇐npnnt(2*i÷
5 67 8 9 10
[= I 2 3 4
I -1=1
breaks
• break is a keyword
used to transfer control
• break is
of loop
terminates the execution of
• break
loop
Write to ask user to
a
program
enter an even number at most 3 times .
chances will be
number ,
then no more
the body
-
It can only be used in
of
loop .
while is -_ so :
2=3
[
if ✗ %2==0 : F- ✗2
continue
) I 2=3
print Ci
" "
✗ = x
, ,
, ,, , ,
, ,, , ✗ =3
3
:
10 ✗ =3
elsewithwhileloop
while condition :
code
code
else :
code
terminates normally .
empty block .
while condition
/
:
if condition :
pass
pass