#3. While Loop

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Full stack web development using python

While loop

Saurabh Shukla (MySirG)


- Agendas
① Iterative control

② while

③ Practice problem
⑥ break
⑤ continue

⑥ else with loop


⑦ pass
ʳ'ⁿ¥É
"
① while

② *.

✗ 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

even natural numbers .

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

outside the loop body .

only be used in the body


• break can

of loop
terminates the execution of
• break

loop
Write to ask user to
a
program
enter an even number at most 3 times .

If user failed to enter an even number

in all the three chances then he has

lost the game .


If user enter an even

chances will be
number ,
then no more

and announced him a winner


given
.
continued
keyword
continue is a

the body
-
It can only be used in
of
loop .

• continue transfers the control


immediately to the next iteration
Exampled
-_ I
number
"
))
x=int(input (
"
Enter a

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

executes when while loop


else block

terminates normally .

won't be execute when


else block
due to break statement
loop terminates
.
pay
pass keyword is used to create

empty block .

while condition

/
:
if condition :

pass
pass

You might also like