CHAPTER-2 GET CREATIVE WITH LOOPS
Topic: How to get output from the Pseudo Code-
1. numbers=[1,3,5,7,13]----- numbers is a Execution-
list variable. Iteration Value of Value of sq Output
sq=0 x
for x in numbers: 1 1 1*1=1 1
sq=x*x 2 3 3*3=9 9
print(x)
3 5 5*5=25 25
• List is a data type that is used to store 4 7 7*7=49 49
multiple values in a single variable. 5 13 13*13=169 169
Hence the output will be- 1 9 25 49 169
2. x=0
while x ix not 10----Step(i)
x=x+1 ------------Step(ii)
print(x)----------- Step(iii)
Execution-
Iteration Value of x Step-(i) Step-(ii) Step-(iii) Output
Test Condition (x
≠10)
1 x=0 0≠10 (True) x=0+1=1 1
2 x=1 1≠10 (True) x=1+1=2 2
3 x=2 2≠10 (True) x=2+1=3 3
4 x=3 3≠10 (True) x=3+1=4 4
5 x=4 4≠10 (True) x=4+1=5 5
6 x=5 5≠10 (True) x=5+1=6 6
7 x=6 6≠10 (True) x=6+1=7 7
8 x=7 7≠10 (True) x=7+1=8 8
9 x=8 8≠10 (True) x=8+1=9 9
10 x=9 9≠10 (True) x=9+1=10 10
11 x=10 10≠10 (False) The loop will be terminated at this
step.
Hence the output will be- 1 2 3 4 5 6 7 8 9 10
3. numbers= [1,2,3]
alphabets=[a,b,c]
for num in numbers: ----------- Outer loop
for alphabet in alphabets: -- Inner loop
print(alphabet)
Execution:
Iteration num alphabet Output
1 num=1 alphabet=a a • Firstly inner loop
(alphabet) executes
2 alphabet=b b
completely.
3 alphabet=c c
4 num=2 alphabet=a a • Then the control will
5 alphabet=b b be back to outer loop.
6 alphabet=c c
•Afterwards value of
7 num=3 alphabet=a a num will change.
8 alphabet=b b
9 alphabet=c c
Output- a b c a b c a b c
Brain Storming
I. Find the Output of the following Pseudo Code:
1. marks= [10,20,30,40,50] 2. x=1
for x in marks: while x is not 5:
x=x+2 sq= x*x
print(x) print(sq)
x=x+1
3. num=3 4. num= [11,22,33,44,55]
x=1 for x in num:
while x is not 10: x=x-5
print(num*x) print(x)
x=x+1
5. numbers= [1,2,3] 6. numx=[1,2,3]
alphabets=[a,b,c] numy=[4,5,6]
for num in numbers: for x in numx:
for alphabet in alphabets: for y in numy:
print(num) print(x)
7. numx=[1,2,3] 8. numx=[1,2,3]
numy=[4,5,6] numy=[4,5,6]
for x in numx: for x in numx:
for y in numy: for y in numy:
print(y) print(x+y)
9. numbers= [1,2,3] 10. numbers= [1,2]
alphabets=[a,b] alphabets=[a,b,c]
for num in numbers: for num in numbers:
for alphabet in alphabets: for alphabet in alphabets:
print(num) print(num)
II. Find the Output of the following expressions:
1. 15%5-3**2+18//3-17/2
2. 14-2%7+3/2 <= 18-9**2+45%4
3. a=5, b=2, c=6
not(a<b) or (b>c) and (c+a<b)
4. a=-3, b=12, c=8
not((a<b) or (b>c) and (c+a<b))
5. 26%5+13**2-15//3+21/7
6. (7>0) and (not(-5<3)) or (15>8)
7. 2**3**2==(2**3)**2
8. (5+3)**2-8/2*(2+2)
9. 3 + 4 * 2 / (1 - 5) ** 2
10. 3==3 or 2==5
11. 10/2 = = 5.0
12. not (3 > 2) and (2 < 5)
13. 2*3**3
14. (True and False) or (False and True)