0% found this document useful (0 votes)
25 views13 pages

CH 9 Flow of Con Sol

Chapter 9 discusses the flow of control in programming, highlighting the importance of sequence, selection, and iteration constructs. It covers concepts such as empty statements, algorithms, while loops, jump statements, and provides examples of code fragments with explanations and corrections. The chapter also includes exercises to rewrite code using different control structures and predicts outputs for given code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views13 pages

CH 9 Flow of Con Sol

Chapter 9 discusses the flow of control in programming, highlighting the importance of sequence, selection, and iteration constructs. It covers concepts such as empty statements, algorithms, while loops, jump statements, and provides examples of code fragments with explanations and corrections. The chapter also includes exercises to rewrite code using different control structures and predicts outputs for given code snippets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CHAPTER 9

FLOW OF CONTROL

TYPE A

2) What is the importance of the three programming constructs?

Answer The three programming constructs are as follow :-


(1)sequence

(2)selection

(3)iteration
3) What is empty statement? What is its need ?

Answer = A statement which does nothing is known as empty statement .

In python empty statement is pass statement .

A pass statement is useful in those place where the syntax of language


requires the presence of a statement but where the logic of the program does
not.
4 ).Which Python statement can be termed as empty statement ?

Answer = pass statement can be termed as empty statement .


5) . What is an algorithm?

Answer = An algorithm is well defined instructions to solve a given problem .

9) What are the four elements of a while loop in Python?

Answer =
The four element of a while loop in python are as follow :
(i) initialization expressions (starting )

(ii) test expression (reporting or stopping)

(iii) the body of the loop (doing)

(iv) update expression (changing )

17) What are jump statements? Name them.

Answer :

Statement that unconditionally transfers program control within a function .

Type of jump statement :

(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 =

It will give an error.

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?

n = int (route "Enter an integer : "))


if n < 1 :
print ("invalid value")
else :
for i in range (1, n + 1) :
print (i * i)
Answer =
It is doing that, if the given number is greater than 1 then it will give the output that
square of number to the ‘n’ terms.
Q8. How are following two code fragments different from one another? Also,
predict the output of following code fragments:
(a)
n = int (input ("Enter an integer : "))
if n > 0 :
for a in range(1, n + n) :
print (a / (n / 2))
else :
print ("Now quitting")

(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.

Output of the code: -


Enter an integer :8
0.25
0.5
0.75
1.0
1.25
1.5
1.75
2.0
2.25
2.5
2.75
3.0
3.25
3.5
3.75
Now quitting

Q9. Rewrite the following code fragments using for loop:


(a)
i = 100
while (i > 0) :
print (i)
i -= 3
(b)
while num > 0 :
print (num % 10)
num = num / 10
(c)
while (num > 0) :
count += 1
sum += num
num -= 2
if count == 10 :
print ( sum / float(count))
break
Answer =
(a)
for i in range (100,0,-3):
print (i)

(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

Q10. Rewrite following code fragments using while loops:


(a)
min = 0
max = num
if num < 0 :
min = num
max = 0 # compute sum of integers from min to max
for i in range (min, max + 1) :
sum += i

(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()

Q11. Predict the output of the following code fragments:


(a)
count = 0
while count < 10 :
print ("Hello")
count += 1
(b)
x = 10
y=0
while x > y :
print (x, y)
x=x-1
y += 1
(c)
keepgoing = True
x = 100
while keepgoing :
print (x)
x = x - 10
if x < 50 :
keepgoing = False

(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:-

for y in range (500, 100, -100) :


print ("*", y)

This code give output: -


* 500
* 400
* 300
* 200

(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?

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 =

Output: -

o+oo+oo++ooo++oo+oo+

*******************FINISH********************

You might also like