print (a)people.iitgn.ac.in/~gmanoj/es112-2019/loops.pdf · i print all numbers from o to 10 in...

53
A = 5 a = a t 1 print (a)

Upload: others

Post on 20-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A = 5

a = a t 1

print (a)

Page 2: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A = 5 a → L F

a = a t 1

print (a)

Page 3: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A = 5 a 55T

a = a t I↳ 1-67

print (a)

Page 4: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

a = 5

a = A t l # c a l l e d i n c remen t

a = a - I # ca l l ed decrement

Page 5: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

I Print a l l numbers from O t o 10 i n o rde r.

Page 6: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

I Print a l l numbers from O t o 10 i n o rde r.

A - O

wh i l e a s - 1 0 :

print (a)a - a t l

Page 7: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

I Print a l l numbers from O t o 10 i n o rde r.

A - O

wh i l e a s - 1 0 :

print (a)a - a - 1 1

While t h e condition a ⇐ 1 0 i s t r u e ,e x e c u t e t h e i n d e n t e d b l o c k .

Page 8: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

I Print a l l numbers from O t o 10 i n o rde r.

A - O

wh i l e a s - 1 0 :

print (a)a - a - 1 1

While t h e condition a ⇐ 1 0 i s t r u e ,e x e c u t e t h e i n d e n t e d b l o c k .

I f t h e condit ion a s - 1 0 i s fa l s e ,e x i t t h e wh i t e loop a n d continuee x e c u t i o n f rom t h e n e x t statement

Page 9: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

a s 0.

wh i l e a s = 1 0 :

print (a)

Page 10: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

a s 0.

wh i l e a c e l o :

print (a)

Infinite loop. because t h e condit ion

9 < = 1 0 i s always t r u e .

Page 11: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Q : pa in t t h e f i r s t 1 0 fibonacci n umb e r s .

Page 12: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Q : pa in t t h e f i r s t 1 0 fibonacci n umb e r s .

O t o L 2 3 5 8 1 3 2 1 3 4

Xo = O

X s = I

X i = X i - i t X i - 2 f o r a l l i z 2 .

Page 13: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

a = 0

print ( a )

b = 1

print (b)

# print 8 m o r e n umb e r s

Page 14: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A = 0

print ( a )

b = 1

print (b)

# print 8 m o r e n umb e r s

i = 3

Whi le

Page 15: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

a = 0

print ( a )

b = 1

print (b)

# print 8 m o r e n umb e r s

i = 3

Whi le i < = 1 0 :

print ( a t b )

Page 16: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A = 0

print ( a )

b = 1

print (b)

# print 8 m o r e n umb e r s

i = 3

Whi le i < = 1 0 :

print ( a t b )temp = a

a = b

b = tempt b

Page 17: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A - O

print ( a )

b - 1

print (b)

# print 8 m o r e n umb e r s

e-= 3

Whi le i < = 1 0 :

print ( a t b ) # a a - b

temp-a b ← a t ba = b

b = tempt b

Page 18: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

A - O

print ( a )

b - 1

print (b)

# print 8 m o r e n umb e r s

e-= 3

Whi le i < = 1 0 :

print ( a t b ) # a - btemp-a b ← a t ba = b

b = tempt bi = i t I

Page 19: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Cs: Ta k e a n u m b e r x . F o r each l e i s a , printi " t " o n l i n e i .

For example: s e e 4*

* to* to t o

t o t o t o t o

Page 20: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Cs: Ta k e a n u m b e r x . F o r each l e i s a , printi " t " o n l i n e i .

For example: s e e 4*

* && & &

& & & t o

N e s t e d W h i l e loop

Page 21: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

w h i l e i i s l e s s t h a n equal t o a

print i * ' s o n l i n e e-

Page 22: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

x = i n t (inputu)i = 1

W h i l e i ⇐ s e e .

print i * ' s o n l i n e e-

Page 23: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = I

w h i l e i s e e :

j = 1w h i l e j ⇐ i :

Page 24: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

w h i l e i ⇐ a :

j = L

w h i l e j < = e i :

print ( " & " , e n d -" " )

j = j H

Page 25: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

w h i l e e' s e - o .

j = L

w h i l e j < = e i :

print ( " & " , e n d -" " )

P.rintfj.FIe-= i t '

Page 26: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

W h i l e e' s e - o .

j = L

w h i l e j a - e . . . }%§in¥§.

print ( " & " , e n d -" " )

Printtjjtie-= i t '

Page 27: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Cs: Ta k e a n u m b e r e . F o r each l e i s a , print" t a " o n l i n e i a s s h o w n b e l o w

For example: s e e 5

- - - - t o ← 1 s t a t e

- - - * * * ← 3 s t a r

- - * * * * * ← 5 s t a r

- * * * * * * t o ← 7 s t a r

* * * * t o * * * * ← g s t a r

Page 28: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Cs: Ta k e a n u m b e r x . F o r each l e i s a , print" t a " o n l i n e i a s s h o w n b e l o w

For example: s e e 5

- - - - t o- - - * * *

- - * * * * *

- * * * * * * t o

* * * * * * * * *

Page 29: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

W h i l e e' s e - o .

j = L

w h i l e j < = e i :

print ( " t o " , e n d -" " )

prints'jJtli - i t '

Page 30: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

W h i l e i ⇐ a :

j = L

p r in t - s p a c e s

pr in t - s t a r s

pr int C )i - i t l

Page 31: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

x = i n t (inputu)i = 1

W h i l e e' s e - o .

j = L

p r in t a - i s p ace spr in t Q i - 1 s t a r s

pr int C )e- = i t

Page 32: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

w h i l e e' s e - o .

j = Lw h i l e ja-e-ei:

print (" "sends"")

j - j e t

p r in t Q i - 1 s t a r s

pr int C )e-= ' e t l

Page 33: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

s e = i n t (inputu)i = 1

W h i l e e' s e - o .

j = Lw h i l e j a - a - i :

paint (" "sends"")

j = 1J-j-11

w h i l e j ⇐ 2 - i - l :- print ( " e " , ends"")Printtijjtl.i - i t l

Page 34: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.

7 6 4 = 7×102+6×10' t 4×100

Page 35: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.

7 6 4 = 7×102+6×101 t 4×100

Any n a t u r a l n u m b e r c a n b e represented usingt h e following 1 0 digits0 I 2 3 4 5 6 7 8 9

Page 36: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.

7 6 4 = 7×102+6×101 t 4×100

Any na t u r a l n u m b e r c a n b e represented usingt h e following 1 0 digits0 I 2 3 4 5 6 7 8 9

B a s e 1 0 o r d e c i m a l

Page 37: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.

Any na t u r a l n u m b e r c a n b e represented usingt h e following 2 digits0 I

B a s e 2 o r Binary

Page 38: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.9 = 1 0 0 1 = 1×23+0×22 t 0×2 ' t 1×20

Any na t u r a l n u m b e r c a n b e represented usingt h e following 2 digits0 I

B a s e 2 o r Binary

Page 39: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

Inpu t : 8

Output a 1 0 0 0

Page 40: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

I f n u m i s d i v i s i b l e by 10 , w h a t i s i t su n i t digit i n d e c i m a l ?

Page 41: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

I f n u m i s d i v i s i b l e by 10 , w h a t i s i t su n i t digit i n d e c i m a l ?

A = O

Page 42: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

I f n u m % 10 = a , w h a t i s t h e u n i t s digitsof n u m in d e c i m a l ?

Page 43: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

I f n u m % 10 = a , w h a t i s t h e u n i t s digitsof n u m in d e c i m a l ?

A = a

Page 44: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

7 6 5

T Tonami.1 0

Page 45: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

7 6 5

T Tonami.1 0 .(num1/10) 1 1 0

Page 46: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

7 6 5

T Tonami.1 0

- I| (num1110) 1 1 0

drum 110/1101%10

Page 47: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

7 = l I I = 1×22 t 1×2' t 1×20

Page 48: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

F = - I 1 I

Tn u m - 1 . 2

Page 49: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.

F = - I 1 I^ ^ T/ (nutmeg,.znum%2Knum12/12/12

Page 50: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.n u m a intlenputo)binary = " "

w h i l e m u m > O :

Page 51: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.n u m = intlenputo)binary = " "

w h i l e m u m > 0 :

l a s tDigit = n u m - 1 . 2

n u m = i n t (numb)

Page 52: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.n u m a intlenputo)binary = " "

w h i l e m u m > 0 :

l a s tDigit = n u m - 1 . 2

n u m = i n t (numb)

binary = binary t s t r l l a s t Digit)

Page 53: print (a)people.iitgn.ac.in/~gmanoj/ES112-2019/Loops.pdf · I Print all numbers from O to 10 in order. A-O while as-10: print (a) a-a-11 While the condition a ⇐ 10 is true, execute

Dec ima l t o Binary.C o n v e r t a n u m b e r f rom dec ima l t o binary.n u m = intlenputo)binary = " "

w h i l e m u m > 0 :

l a s tDigit = n u m - 1 . 2

n u m = i n t (numb)

binary = binary t strllastDigit)

print (binary C::-D)