Class 11 Computer Science 083
Ch 9: Flow of Control
Check Point 9.3
Sumita Arora Book Exercise Solution
Que 1. What are Iteration Statement? Name the iteration statements provided by Python.
Answer: The iteration statements or repetition statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled.
- The iteration statement is also called loops or looping statements.
Python provides two types of loops
- (a) for loop – Counting loop, the loops that repeat a certain number of times;
- (b) while loop – Conditional loop, the loops that repeats as long as condition is true.
Que 2. What are the two categories of Python loops?
Answer: Two categories of Pythn loops are:
- (a) for loop – Counting loop, the loops that repeat a certain number of times;
- (b) while loop – Conditional loop, the loops that repeats as long as condition is true.
Que 3. What is the use of range( ) function ? What would range(3, 13) return ?
Answer: The range() function of Python generates a list which is a sequence of integers.
Syntax:
range (start_value, stop_value, step_value)
Where start and step is optional, by default start value is 0 and step value is 1.
- range(n) will genearte a sequence starting from 0, 1, 2, …., n-1.
- range(l, u) will generate a squence starting from l, l+1, l+2, …, u-1.
- range(l, u, s) will generate a sequence starting from l, l+s, l+2s, l+3s, …, u-1
Note : start_value is included in the list but stop_value is not included in the list.
For example:
range(5) => generate a sequence [0, 1 , 2, 3, 4]
range(4, 9) => generate a sequence [4, 5, 6, 7, 8]
range(10, 20, 3) => generate a sequence [10, 13, 16, 19]
Que 4. What is the output of following code fragment?
for a in "abcde" :
print(a, '+', end = ' ')
Answer: a + b + c + d + e +
Explanation:
- In the given for loop, the sequence is “abcde”.
- The for loop itereate the code for each value of sequence “abcde”.
- In each iteration a will get one character from the sequence ‘a’, ‘b’, ‘c’, ‘d’, ‘e’.
- print() statement print the value of a and ‘+’ in each itereation. The end = ‘ ‘ parameter of print keeps the cursor in the same line and insert the space after ‘+’ symbol.
Que 5. What is the output of following code fragment?
for i in range(0, 10) :
pass
print(i)
Answer: 9
Explanation:
- In the given for loop, the range(0, 10) generates a sequence [0, 1, 2, 3,4, 5, 6, 7, 8, 9]
- The for loop itereate the code for each value of sequence [0, 1, 2, 3,4, 5, 6, 7, 8, 9]
- Inside the loop pass statement is written which does nothing, just move to the next itereation.
- The last value of i is 9, so the print() statement print the value of i is 9.
Que 6. Why does “Hello” not print even once ?
for i in range(10, 1) :
print("Hello")
Answer: Because the range(10, 1) generate an empty sequence [ ]. So the loop will not execute/iterate for a single times.
Que 7. What is the output of following code ?
for a in range (2, 7) :
for b in range (1, a) :
print ('#', end = ' ')
print( )
Answer: Output is:-
#
# #
# # #
# # # #
# # # # #
Que 8. Write a for loop that displays the even numbers from 51 to 60.
Answer: Program is –
for a in range (52, 61, 2) :
print (a , end = ' ')
OR
for a in range (51, 61) :
if a % 2 == 0:
print (a , end = ' ')
Que 9. Suggest a situation where an empty loop is useful.
Answer: Empty loop is useful when we want a loop but it does not do any thing. Like a loop for waiting, loop for counting, etc.
Que 10. What is the similarity and differences between for and while loop ?
Answer: Similarity:-
- Both for() and while () loop is execute the set of statements more than one times till conditioin is True.
Difference:
- for loop is used to execute the set of statements for finite number of times, known as counting loop.
- while loop is used to execute the set of statements as long as the condition is true, known as conditional loop.
Que 11. Why is while loop called an entry controlled loop ?
Answer: Entry controlled loop means a loop which check the condition before the execution of loop body.
In Python, while loop does the same thing means it check the condtion before the execution of loop body.
That’s why while( ) loop is called an Entry controlled loop.
Que 12. If the test-expression of a while loop evaluates to false in the beginning of the loop, even before entering in to the loop :
- (a) how many times is the loop executed?
- (b) how many times is the loop-else clause executed?
Answer: (a) Zero
(b) One
Que 13. What is the output of following code ?
while( 6 + 2 > 8 ) :
print("Gotcha!")
else:
print("Going out!")
Answer: Output is:
Gotcha!
Going Out!.
Explanation:
- 6 + 2 > 8 returns False, it executed like 8 > 8 is False
- else statement of loop is executed when control is going out from the loop.
- Note : Arithmetic operator evaluated first than relational opeartor
Que 14. What is the output of following code ?
for a in range (2, 7) :
for b in range (1, a) :
print ( b , end = ' ')
print( )
Answer: Output is:-
1
1
2
1
2
3
1
2
3
4
1
2
3
4
5
Que 15. What is the significance of break and continue statements ?
Answer: break and continue, both are the jump statement.
In Python, jump statement is used to jump the control from one position to the another position. In Python, there are two types of jump statements. These are –
break :
- break – A break statement terminates the very loop it lies within, executioin resumes at the statement immediately following the body of the terminated statement.
- It enables a program to skip over a part of the code.
- It jumps the control outside the loop.
continue:
- A continue statement forces the next iteration of the loop to take place, skipping any code in between.
- It jumps the control to the beginning of the loop for the next iteration.
Que 16. Can a single break statement in an inner loop terminate all the nested loops ?
Answer: No