Chapter 6 Repetition Control Instructions¶
Keynote¶
6.1 Repetition Control Instructions (Zei dah an si?)¶
Program chungah statement (tuah dingmi) pawl a nawn in tuah á¹han lengmang kan duh tikah hman a si. Repetition control instruction phun hnih a um:
- while
- for
Language dang (C, C++, Java) bantuk in do-while loop hi Python ah a um lo.
while loop cu condition a hman (true) chung paoh kha instruction a tuah á¹han lengmang. Phun hnih in kan hman khawh:
1. The while Loop¶
while loop cu condition a hman (true) chung paoh kha statement pawl a tuah á¹han lengmang. Condition kha thil tuah hlan ah a check hmasa, cucaah entry-controlled loop tiah auh a si.
A Umtu Ning (Syntax):
Phun hnih in while loop hi hman khawh a si:
A. A Umtu Ning Pang (Basic Structure)
while condition:
# Loop chungah tuah dingmi
statement1
statement2
# Condition kha a donghnak ah False a si a hau,
# lo cun a tawp ti lomi (infinite loop) a chuak lai!
elseblock hi hman a hau hrimhrim lo (optional). Asinain kan hman ahcun, condition a 'false' tikah a chung i statement pawl kha a tuah lai.breakstatement hman inwhileloop kha kan hrawh/donghter sual ahcun,elseblock kha a tuah lai lo.
for cu string, tuple asiloah list bantuk sequence (thil tlar) chung i element pawl pakhat hnu pakhat lakin tuah á¹han lengmang nak ah hman a si. Phun hnih in a um:
- Loop a kal fatin
varkhalistchung in a changtu value (hmet) pek a si. listhmun ahstring,tuple,setasiloahdictionaryzong hman khawh a si.elseblock cu hman a hau hrimhrim lo (optional). Kan hman ahcun, break hman in loop kan hrawh lo poh ahcunelsechung i statement pawl kha a tuah lai.
6.2 While loop hman ning¶
while loop hi a tanglei thil sining pathum ah hman khawh a si:
- Condition a hman (True) chung paoh statement pawl tuah á¹han lengmang.
- Statement pawl kha kan duh zat tiang (voi zeizat dah ti hngalh cia in) tuah á¹han lengmang.
- String, list, le tuple chung i element pawl lakin kal.
Condition a hman chung paoh statement tuah á¹han lengmang kan ti tikah, statement kha voi zeizat dah tuah a hau lai ti a hngalh cia lomi caah hman a si.
num = int(input('Enter a number: '))
while num != 5 :
print(num, num * num)
num = int(input('Enter a number: '))
Statement pawl kha voi zeizat dah tuah ding ti hngalh cia in (finite number of times) tuah á¹han lengmang nan while loop kan hman khawh.
while loop cu index nambat (index value) hman in string, list, asiloah tuple chung i thil ummi pawl lakin kal nan hman khawh a si, a tanglei program ah kan hmuh bantuk in:
s = 'Mumbai'
lst = ['desert', 'dessert', 'to', 'too', 'lose', 'loose']
tpl = (10, 20, 30, -20, -10)
i = 0
while i < len(lst) :
print(i, s[i], lst[i], tpl[i])
i += 1
Theih ding (Note):
Hi program ah hin tpl (tuple) hi element 5 lawng a ngei, asinain lst (list) hi element 6 a ngei. Loop hi lst sau (length) chung a kal lai caah, a donghnak ah tpl index a tlauh sual (IndexError) kho.
SetasiloahDictionarychung i thil pawl cu index nambat hman in lak khawh an si lo caah,forloop hman hi a á¹ha deuh.
Cunglei i while loop hman ning pathum kan hmuhmi chung ah, a pakhatnak (voi zeizat dah tuah ding hngalh cia lo, condition hman chung paoh) hi hman a tam bik a si. A dang pahnih cu for loop hman in tuah a tlangpi a si.
6.3 For loop hman ning¶
for loop cu a tanglei thil sining pahnih ah hman khawh a si:
- Statement pawl kha voi zeizat dah tuah ding ti hngalh cia in (finite number of times) tuah á¹han lengmang.
string,list,tuple,setasiloahdictionarychung i thil ummi pawl lakin kal.
Statement pawl kha voi zeizat dah tuah ding ti hngalh cia in tuah á¹han lengmang kan duh tikah, a chungah a um ciami function range() hi hman a si.
range() function nih integer (nambat tling) tlar a chuah.
range(10)- 0 in 9 tiang nambat a chuah.range(10, 20)- 10 in 19 tiang nambat a chuah.range(10, 20, 2)- 10 in 19 tiang, 2 in karh in a chuah (10, 12, 14...).-
range(20, 10, -3)- 20 in 9 tiang, 3 in zor in a chuah (20, 17, 14...). -
Theih ding:
range()nihfloat(point nambat) tlar a chuah kho lo.
A tlangpi in,
range(start, stop, step)
Hi nih hin integer nambat tlar kha start in a thawk lai i stop tiang a kal lai (asinain stop nambat taktak kha a tel lo/exclusive), cun step zat in a karh/zor lai.
range() hman in nambat tlar kan chuahmi pawl kha for loop hman in kan lak khawh.
for char in 'Leopard' :
print(char)
for animal in ['Cat', 'Dog', 'Tiger', 'Lion', 'Leopard'] :
print(animal)
for flower in ('Rose', 'Lily', 'Jasmine') :
print(flower)
for num in {10, 20, 30, -10, -25} :
print(num)
for key in {'A101' : 'Rajesh', 'A111' : 'Sunil', 'A112' : 'Rakesh'} :
print(key)
for loop ah khan, loop a kal fatin char kha string chung in a changtu value (cafang) pek a si.
Cu bantuk á¹hiamá¹hiam in, pahnihnak, pathumnak le palinak for loop ah, loop a kal fatin animal/flower/num kha list/tuple/set chung in a changtu value pek an si.
- Theih ding: A donghnak
forloop ah dictionarykey(a bi) lawng kan chuah (print) kha na hmuh lai. A value (a man) chuah, asiloah a key le a value chuah á¹i kha cu Chapter 11 ah kan cawng te lai.
for loop hman in thil tlar chung kan kal lio ah, a umnak hmun (index) zong hngalh chih kan duh ahcun, a tanglei bantuk in a chungah a um ciami enumerate() function hi hman ding a si:
lst = ['desert', 'dessert', 'to', 'too', 'lose', 'loose']
for i, ele in enumerate(lst) :
print(i, ele)
6.4 break le continue¶
breaklecontinuestatement pawl hiwhileleforhe hman khawh an si.breakstatement nihelseblock tuah lo in loop kha a donghter/hrawh.continuestatement nih block chung i statement dang pawl kha a hlut (skip) i, loop a changtu iteration ah a kal colh.
6.5 Loop i Else Block¶
whileloop ielseblock cu, loop kha a pang tein a dih (normally terminated) lawngah thil tuah ter kan duh i,breakhman in a tawp tikah tuah ter kan duh lo caan ah hman a si.- Cu bantuk thil sining cu nambat pakhat hi prime number a si le si lo kan check tikah a chuak tawn.
num = int(input('Enter an integer: '))
i = 2
while i <= num - 1 :
if num % i == 0 :
print(num, 'is not a prime number')
break # Hika ah a break ahcun else block a tuah lai lo
i += 1
else :
print(num, 'is a prime number')
else kan á¹ialnak hmun (indentation) kha á¹ha tein zoh. Hi else hi while loop ta a si, if statement ta a si lo.
- A tanglei tahchunhnak ah hin
elseblock cu a tuah lai lo, zeicatiah list chungah3a um.3cu 10 in hrawm khawh a si lo (non-multiple), cucaah3kan hmuh bak in loop kha kan hrawh (break) lai.
for ele in [10, 20, 30, 3, 40, 50] :
if ele % 10 != 0 :
print(ele, 'is a not a multiple of 10')
break
else :
print('all numbers in list are multiples of 10')
6.6 Nested Loops (Loop chungah loop dang)¶
- Loop pakhat chungah loop dang kan hman chih khawh.
- A leng loop (outer loop) voi khat a kal fatin, a chung loop (inner loop) kha a tling tein (a thawk in a dongh tiang) a kal dih lai.
Problems¶
Problem 6.1¶
p, n, le r value set 3 la law, set pakhat cio caah simple interest tuaknak program á¹ial.
Program
Output
Problem 6.2¶
Problem 6.2¶
Infinite loop hman in nambat 1 in 10 tiang chuahnak (print) program á¹ial. Nambat vialte kha tlar khat (same line) ah an chuak dih lai.
Tips
while 1nih infinite loop a ser, zeicatiah 1 cu 'zero' a si lo, cucaah 'true' in a hmuh.- 1 hmun ah nambat dang (zero a si lomi paoh) na hman ahcun infinite loop a ser thiamthiam lai.
- Infinite loop sernak lam dang cu
while Truehman hi a si. print()chung iend = ' 'nih khan iteration pakhat a kal fatinia chuah (print) hnu ah 'space' a chiah. A sawhsawh (default) in cunendhinewline('\n') a si.
Problem 6.3¶
Nambat 1, 2 le 3 hman in an i lo lomi (unique) combinations vialte chuahnak program á¹ial.
Program
Problem 6.4¶
Binary numeric string (nambat '0' le '1' lawng ummi) pakhat la law, cu nambat i a decimal value (kan hman tawnmi nambat) kawlnak program á¹ial. Tahchunhnak ah, '1111' i a decimal value cu 15 a si.
Program
Tips
ord('1')cu 49 a si,ord('0')cu 48 a si. (Cucaahord('1') - ord('0')kan tuah tikah1a chuak,ord('0') - ord('0')tikah0a chuak).b = b[1:]nihbstring chung i a hmasa bik cafang kha a hlonh (strip).
Problem 6.5¶
for loop hman in a tanglei bantuk cafang pawl chuahnak (output) program á¹ial:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,
z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a,
Program
Output
Tips
- Cafang A-Z i an Unicode value (nambat) cu 65 in 90 an si. Cafang a-z i an Unicode value cu 97 in 122 an si.
- Print statement in a chuakmi paoh a donghnak ah comma (,) a tel lai.
- A chungah zeihmanh a um lomi
print( )statement nih cursor kha tlar thar (next line) a thawknak ah a chiah.
Exercise¶
[A] Answer the following questions (A tanglei biahalnak pawl hi phi):¶
a. When does the else block of a while loop go to work?
(while loop i else block kha zeitik ah dah a tuah?)
- Phi:
whileloop ielseblock cu loop condition kha a pang tein a False tikah (loop a dih tikah) tuah a si. Asinain, loop khabreakstatement hman in kan hrawh (terminate) ahcunelseblock cu a tuah lai lo.
b. Can range( ) function be used to generate numbers from 0.1 to 1.0 in steps of 0.1?
(range() function hi 0.1 in 1.0 tiang, 0.1 in karh nakhnga hman khawh a si maw?)
- Phi: A ngah lo. Python ah
range()function cu Integer (nambat tling) lawng caah hman khawh a si. Point nambat (float) caah cunnumpybantuk library hman a hau.
c. Can a while loop be nested within a for loop and vice versa?
(while loop chungah for loop chiah a ngah maw, asiloah a let in a ngah maw?)
- Phi: A ngah. Loop pakhat chungah loop phun dang chiah (Nested) hi a ngah.
d. Can a while/for loop be used in an if/else and vice versa?
(if/else chungah loop hman a ngah maw, asiloah loop chungah if/else hman a ngah maw?)
- Phi: A ngah. Control instruction pawl (loop, if-else) cu duh paoh in chawh in hman (nest) khawh an si.
e. Can a do-while loop be used to repeat a set of statements?
(Statement pawl tuah á¹han lengmang nakhnga do-while loop hman khawh a si maw?)
- Phi: A ngah lo. Python language ah do-while loop a um lo.
f. How will you write an equivalent for loop for the following:
(A tanglei while loop he a i tlukmi for loop zeitindah na á¹ial lai?)
(g) What will be the output of the following code snippet? (A tanglei code snippet nih zei output dah a chuah lai?)
* Phi: 20 17 14 11(h) Why should break and continue be always used with an if embedded in a while or for loop?
(Zeicah break le continue hi while asiloah for loop chungah if he (condition he) hman a herh zungzal?)
- Phi:
if(condition) tel lo inbreakasiloahcontinuekan hman ahcun, loop a kal hmasa bik (first iteration) ah a tawp colh lai (break) asiloah a hlut colh lai (continue). - Cu tikah, loop chung i statement dang pawl kha zeitik hmanh ah a tuah kho ti lai lo (Unreachable code an si lai).
- Cucaah, thil sining (condition) pakhatkhat a tlin lawngah loop hrawh/hlut kan duh tawn caah
ifhe hman a herh.
[B] Point out the errors, if any, in the following programs:¶
(A tanglei program pawl ah hin a palhmi an um ahcun kawl/chim:)
a.
b.
c. d. e. f. g.[C] Match the following for the values each range( ) function will generate¶
(A tanglei range() function cio nih a chuah dingmi value (nambat tlar) pawl hi, a hmanmi he pehtlaih hna.)
a. range(5) 1. 1, 2, 3, 4
b. range(1, 10, 3) 2. 0, 1, 2, 3, 4
c. range(10, 1, -2) 3. Nothing
d. range(1, 5) 4. 10, 8, 6, 4, 2
e. range(-2) 5. 1, 4, 7
[D] Attempt the following questions:¶
(A tanglei biahalnak pawl hi phi i zuam:)
a. Write a program to print first 25 odd numbers using range( ).
(range() function hman in odd number (nambat cawh) a hmasa bik 25 chuahnak program á¹ial.)
b. Rewrite the following program using for loop.
(for loop hman in a tanglei program hi á¹ial á¹han.)
lst = ['desert', 'dessert', 'to', 'too', 'lose', 'loose']
s = 'Mumbai'
i = 0
while i < len(lst) :
if i > 3 :
break
else :
print(i, lst[i], s[i])
i += 1
d. A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not. (Keyboard in digit-5 ummi nambat á¹ial a si. Cu nambat cu letthlit (reverse) in tuahnak le, a letthlit kan tuahmi le a hramthawk nambat kha an i luk maw luk lo ti check-nak program á¹ial.)
e. Write a program to find the factorial value of any number entered through the keyboard. (Keyboard in á¹ialmi nambat paoh i a 'factorial value' kawlnak program á¹ial.)
f. Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ). (1 le 500 kar ah a ummi Armstrong number vialte chuahnak program á¹ial. Nambat pakhat i a digit paoh kha voi 3 karh (cube) in kan fonh (sum) tikah, a hramthawk nambat he an i luk ahcun, cu nambat cu Armstrong number ti a si. Tahchunhnak: 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ).)
g. Write a program to print all prime numbers from 1 to 300. (1 in 300 kar ah a ummi prime number vialte chuahnak program á¹ial.)
h. Write a program to print the multiplication table of the number entered by the user. The table should get displayed in the following form: (User nih a á¹ialmi nambat i a a-li (multiplication table) chuahnak program á¹ial. Table cu a tanglei bantuk in langhter ding:)
i. When interest compounds q times per year at an annual rate of r % for n years, the principal p compounds to an amount a as per the following formula:
\(a = p ( 1 + \frac{r}{q} )^{nq}\)
Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
(Kum khat ah q voi a karh (compound) mi interest r% in kum n chung a um tikah, a hram p cu a tanglei formula ning in a ah a cang: \(a = p ( 1 + \frac{r}{q} )^{nq}\). p, r, n le q set 10 la law, a chuakmi a tuaknak program á¹ial.)
j. Write a program to generate all Pythagorean Triplets with side length less than or equal to 30. (Sir (side) pakhat cio 30 nak in a tlawm deuh asiloah a tlukmi Pythagorean Triplets vialte chuahnak program á¹ial.)
k. Population of a town today is 100000. The population has increased steadily at the rate of 10 % per year for last 10 years. Write a program to determine the population at the end of each year in the last decade. (Tuchun ah khua pakhat i milu cu 100,000 an si. A luan ciami kum 10 chung khan, kum khat ah 10% in hma a panh (karh) lengmang. A luan ciami kum 10 chung i kum khat a dih fatin milu zeizat dah an si ti kawlnak program á¹ial.)
l. Ramanujan number is the smallest number that can be expressed as sum of two cubes in two different ways. Write a program to print all such numbers up to a reasonable limit. (Ramanujan number timi cu nambat pahnih i an cube (voi thum karh) fonh in, lam phun hnih in hmuh khawhmi nambat hme bik a si. Cu bantuk nambat pawl cu a niamnak tawk (reasonable limit) tiang chuahnak program á¹ial.)
m. Write a program to print 24 hours of day with suitable suffixes like AM, PM, Noon and Midnight. (Nikhat chung suimilam 24 kha AM, PM, Noon le Midnight tiah a hmanmi á¹hiam in chuahnak program á¹ial.)