CH 9 Flow of Con Sol
CH 9 Flow of Con Sol
FLOW OF CONTROL
TYPE A
(2)selection
(3)iteration
3) What is empty statement? What is its need ?
Answer =
The four element of a while loop in python are as follow :
(i) initialization expressions (starting )
Answer :
(i)break
(ii)continue
(iii)return
TYPE B
Q. Rewrite the following code fragment that saves on the number of
comparisons:
if (a == 0) :
print ("Zero")
if (a == 1) :
print("One")
if (a == 2) :
print ("Two")
if (a == 3) :
print ("Three")
Answer =
if (a == 0) :
print ("Zero")
elif (a == 1) :
print("One")
elif (a == 2) :
print ("Two")
elif (a == 3) :
print ("Three")
Q. Under what conditions will this code fragment print "water"?
if temp < 32 :
print ("ice")
elif temp < 212 :
print ("water")
else :
print ("steam")
Answer =
If value of temp is in between 32 and 212. Then the output will be ‘water’.
Q3. What is the output produced by the following code?
x=1
if x > 3 :
if x > 4 :
print ("A", end = ' ')
else :
print ("B", end = ' ')
elif x < 2:
if (x != 0) :
print ("C", end = ' ')
print ("D")
Answer =
Output is:-
CD
Q4. What is the error in following code? Correct the code:
weather = 'raining'
if weather = 'sunny' :
print ("wear sunblock")
elif weather = "snow" :
print ("going skiing")
else :
print (weather)
Answer =
weather = 'raining'
if weather == 'sunny' :
print ("wear sunglasses")
elif weather == "snow" :
print ("going skitting")
else :
print (weather)
Q5. What is the output of the following lines of code?
if int('zero') == 0 :
print ("zero")
elif str(0) == 'zero' :
print (0)
elif str(0) == '0' :
print (str(0))
else :
print ("none of the above")
Answer =
Q6. Find the errors in the code given below and correct the code:
if n == 0
print ("zero")
elif :n == 1
print ('one')
elif
n == 2:
print("two")
else n == 3 :
print ("three")
Answer =
if n == 0 :
print ("zero")
elif n == 1 :
print ('one')
elif n == 2 :
print("two")
elif n == 3 :
print ("three")
Q7. What is following code doing? What would it print for input as 3?
(b)
n = int (input( "Enter an integer :"))
if n > 0 :
for a in range(1, n + n) :
print (a / (n / 2))
else:
print ("Now quitting")
Answer =
There is no difference between these codes.
If you found difference then, comment the difference in this post.
(b)
for i in range (n):
print (n % 10)
n = n / 10
if n < 0 :
break
(c)
for i in range (num,0,-2):
print(num)
count += 1
sum += num
if count == 10 :
print ( sum / float(count))
break
(b)
for i in range(1, 16) :
if i % 3 == 0 :
print (i)
(c)
for i in range(4) :
for j in range(5) :
if i +1 == j or j + i == 4 :
print ("+", end = " ")
else :
print ("o", end = " ")
print ()
Answer =
#(a)
min = 0
max = num
if num < 0 :
min = num
max = 0 # compute sum of integers from min to max
i = min
while i < max + 1 :
sum += i
i += 1
#(b)
i = 1
while i < 16 :
if i % 3 == 0 :
print (i)
i += 1
#(c)
i = 0
j = 0
while i < 4 :
j = 0
while j < 5 :
if i + 1 == j or j + i == 4 :
print("+",end=" ")
j += 1
else :
print("o",end = " " )
i += 1
print()
(d)
x = 45
while x < 50 :
print (x)
(e)
for x in [1,2,3,4,5] :
print (x)
(f)
for x in range (5) :
print (x)
(g)
for p in range(1, 10) :
print (p)
(h)
for q in range (100, 50, -10) :
print (q)
(i)
for z in range (-500, 500, 100) :
print (z)
(j)
for y in range (500, 100, 100) :
print ("*", y)
(k)
x = 10
y=5
for i in range (x - y * 2) :
print ("%", i)
(l)
for x in [1, 2, 3] :
for y in [4, 5, 6] :
print (x, y)
(m)
for x in range(3) :
for y in range (4) :
print (x, y, x + y)
(n)
c=0
for x in range (10) :
for y in range (5) :
c += 1
print (c)
Answer =(a)
Output:-
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
(b)
Output: -
10 0
91
82
73
64
(c)
Output: -
100
90
80
70
60
50
(d)
Output: -
45
45
45
45
45
45
45
45
45 infinite times.
(e)
Output: -
1
2
3
4
5
(f)
Output: -
0
1
2
3
4
(g)
Output: -
1
2
3
4
5
6
7
8
9
(h)
Output: -
100
90
80
70
60
(i)
Output: -
-500
-400
-300
-200
-100
0
100
200
300
400
>>>
(j)
It will give no output.
After some change in code like:-
(k)
It will give no output, because precedence of * operator is more than - operator.
So for value in range become zero after solving.
(l)
Output: -
14
15
16
24
25
26
34
35
36
>>>
(m)
Output: -
000
011
022
033
101
112
123
134
202
213
224
235
>>>
(n)
Output: -
50
Q13. What is the output of the following code?
if i + 1 == j or j + i == 4 :
else :
print ()
Answer =
Output: -
o+oo+oo++ooo++oo+oo+
*******************FINISH********************