NCERT Solutions Class 11, Computer Science, Chapter- 6, Flow of Control
Exercise
1. What is the difference between else and elif construct of if statement?
Solution:
‘Else’ is used along with ‘if’ to define the alternative path if the condition mentioned in the ‘if’ statement is incorrect. This means that only one statement can be evaluated using the if..else statement.
If more than one statements need to be evaluated, the ‘elif’ construct is used. There is no limit on the number of ‘elif’ constructs that should be used in a statement. However, the conditions mentioned in each ‘elif’ construct should be mutually exclusive i.e. if one of the conditions in if..elif is correct it should imply that all the other conditions are false.
2. What is the purpose of range() function? Give one example.
Solution:
The range() function is a built-in function of Python. It is used to create a list containing a sequence of integers from the given start value to the stop value (excluding the stop value). This is often used in for loop for generating the sequence of numbers.
Example:
for num in range(0,5):
print (num)
OUTPUT:
0
1
2
3
4
3. Differentiate between break and continue statements using examples.
Solution:
The 'break' statement alters the normal flow of execution as it terminates the current loop and resumes execution of the statement following that loop.
Example:
num = 0
for num in range(5):
num = num + 1
if num == 3:
break
print('Num has value ' , num)
print('Encountered break!! Out of loop')
OUTPUT:
Num has value 1
Num has value 2
Encountered break!! Out of loop
When a 'continue' statement is encountered, the control skips the execution of remaining statements inside the body of the loop for the current iteration and jumps to the beginning of the loop for the next iteration. If the loop’s condition is still true, the loop is entered again, else the control is transferred to the statement immediately following the loop.
#Prints values from 0 to 5 except 3
num = 0
for num in range(5):
num = num + 1
if num == 3:
continue
print('Num has value ' ,num)
print('End of loop')
OUTPUT:
Num has value 1
Num has value 2
Num has value 4
End of loop
4. What is an infinite loop? Give one example.
Solution:
The statement within the body of a loop must ensure that the test condition for the loop eventually becomes false, otherwise, the loop will run infinitely. Hence, the loop which doesn’t end is called an infinite loop. This leads to a logical error in the program.
Example:
num = 20
while num > 10:
print(num)
num = num + 1
The above statement will create an infinite loop as the value of ‘num’ initially is 20 and each subsequent loop is increasing the value of num by 1, thus making the test condition num > 10 always true.
5. Find the output of the following program segments:
(i) a = 110
while a > 100:
print(a)
a -= 2
Solution:
The value of ‘a’ is decreased by 2 on each iteration of the loop and when it reaches 100, the condition a >100 becomes false and the loop terminates.
OUTPUT:
110
108
106
104
102
(ii) for i in range(20,30,2):
print(i)
Solution:
The range(start, stop, step) function will create a list from the start value to the stop value(excluding the stop value) with a difference in the step value.
The list thus created will be [20, 22, 24, 26, 28].
OUTPUT:
20
22
24
26
28
(iii) country = 'INDIA'
for i in country:
print (i)
Solution:
The variable 'country' will act as a list of characters for the loop to iterate.
OUTPUT:
I
N
D
I
A
(iv) i = 0; sum = 0
while i < 9:
if i % 4 == 0:
sum = sum + i
i = i + 2
print (sum)
Solution:
The ‘while’ loop will run till the value of 'i' is 8, and the condition in the ‘if’ function will return true for two values i.e. 4 and 8, which will be added to ‘sum’. The last statement will print the value of the variable ‘sum’.
OUTPUT:
12
(v) for x in range(1,4):
for y in range(2,5):
if x * y > 10:
break
print (x * y)
Solution:
The first loop will iterate over the list [1, 2, 3]
The second for loop will iterate over the list [2, 3, 4]
The print statement is printing the value of x * y i.e. [1*2, 1*3, 1*4, 2*2, 2*3, 2*4, 3*2, 3*3, 3*4] which comes out to be [2, 3, 4, 4, 6, 8, 6, 9, 12].
The break statement will terminate the loop once the value of x * y is greater than 10, hence, the output value will be till 9.
OUTPUT:
2
3
4
4
6
8
6
9
(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
Solution:
OUTPUT:
Current variable value: 7
Current variable value: 5
Good bye!
Current variable value: 4