Control Statements (Loops & Nested Loops)

ICSE Class 9 Robotics & AI · Solutions for Class 9 ICSE Exploring Robotics & Artificial Intelligence · 55 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

Which of the following is not used as loop in Python?

Show SolutionHide Solution↓

do-while loop

Reason — Python does not support do-while loop as a built-in looping construct. Only for loop and while loop are used to perform iterative tasks in Python.

21 Mark · Multiple Choice Questions

Which of the following statement is a fixed iterative loop?

Show SolutionHide Solution↓

for

Reason — The for loop is the best example of a fixed iterative loop because the number of iterations is known and fixed in advance using the range() function or any other iterable.

31 Mark · Multiple Choice Questions

How is the range() function typically used in a for loop?

Show SolutionHide Solution↓

range(start, end, step)

Reason — The range() function can be used with one, two or three parameters. The complete form is range(start, end, step) where start is the initial value, end is the stop value and step is the update value.

41 Mark · Multiple Choice Questions

Which of the following loop does not execute even once, if the condition is false in the beginning?

Show SolutionHide Solution↓

while

Reason — The while loop checks the condition before executing the loop body. If the condition is false at the very beginning, the control will not enter the loop block and hence the loop will not execute even once.

51 Mark · Multiple Choice Questions

Which of the following statements is most appropriate to illustrate the difference between a for loop and a while loop?

Show SolutionHide Solution↓

A for loop is used for fixed iterations, while a while loop is used for conditional statements.

Reason — The for loop is a fixed iterative loop where the number of iterations are known in advance. The while loop is an unfixed iterative loop which executes based on a conditional expression and continues until the condition becomes false.

61 Mark · Multiple Choice Questions

Which of the following statement holds valid with Python for loop?

Show SolutionHide Solution↓

stop value is mandatory

Reason — The for loop is a fixed iterative loop where the number of iterations is determined using the range() function. In range(), the initial value and step value are optional because their default values are 0 and 1 respectively, but the stop value is mandatory as it specifies the ending limit of the loop.

71 Mark · Multiple Choice Questions

Which of the following loop does not need to check the condition before the execution begin?

Show SolutionHide Solution↓

for

Reason — The for loop works with a predefined sequence or iterable, so the number of iterations is fixed in advance unlike the while loop which depends on a condition.

81 Mark · Multiple Choice Questions

The purpose of the range function in a for loop is to ...............

Show SolutionHide Solution↓

generate a sequence of numbers

Reason — The range() function is a built-in function that generates a sequence of integer numbers based on the initial value, stop value and update parameters. This sequence is then used by the for loop for iteration.

91 Mark · Multiple Choice Questions

To find the sum of whole numbers up to 10, a loop runs:

Show SolutionHide Solution↓

eleven times

Reason — Whole numbers start from 0. Therefore, the whole numbers up to 10 are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, which makes a total of 11 numbers. Hence, the loop runs eleven times.

101 Mark · Multiple Choice Questions

Using ............... loop, an infinite loop can be created.

Show SolutionHide Solution↓

while

Reason — An infinite loop can be created with the help of the while loop by giving an ever-true condition. Since the condition will never become false, the loop will keep on executing infinitely.

111 Mark · Multiple Choice Questions

A loop statement is given as:

for i in range(10):

For how many times will the given loop statement be executed?

Show SolutionHide Solution↓

10 times

Reason — The function range(10) generates a sequence of numbers from 0 to 9, which contains 10 values. Hence, the loop will execute 10 times.

121 Mark · Multiple Choice Questions

When a code allows to use one loop inside another loop, known as ...............

Show SolutionHide Solution↓

nested loop

Reason — A loop written inside another loop is called a nested loop. In this type of loop, the inner loop executes completely for every iteration of the outer loop.

1Predict Output — Snippets

Predict the output of the following snippet:

m, n = 2, 15
for i in range(1, 5):
    m = m + 1
    n = n - 1
print("m =", m)
print("n =", n)
Show SolutionHide Solution↓
m = 6
n = 11

Let’s break down the code line by line:

1. m, n = 2, 15: This statement uses multiple assignment, where m is assigned 2 and n is assigned 15 in a single statement.

2. for i in range(1, 5):: The for loop runs for the values 1, 2, 3, 4. Therefore, the loop executes 4 times.

3. Iterations

  • First Iteration: m = 3, n = 14
  • Second Iteration: m = 4, n = 13
  • Third Iteration: m = 5, n = 12
  • Fourth Iteration: m = 6, n = 11

4. print("m =", m): Displays m = 6

5. print("n =", n): Displays n = 11

6. Therefore, the final output is:

m = 6
n = 11
2Predict Output — Snippets

Predict the output of the following snippet:

k = 1
i = 2
while(i < 6):
    k = k * i
    i = i + 1
print(k)
Show SolutionHide Solution↓
120

Let’s break down the code line by line:

1. k = 1: The variable k is assigned the value 1.

2. i = 2: The variable i is assigned the value 2.

3. while(i < 6):: The while loop iterates as long as i < 6.

4. The iterations are as follows:

  • i = 2: k = 1 * 2 = 2, then i becomes 3
  • i = 3: k = 2 * 3 = 6, then i becomes 4
  • i = 4: k = 6 * 4 = 24, then i becomes 5
  • i = 5: k = 24 * 5 = 120, then i becomes 6

5. When i = 6, the condition becomes false and the loop terminates. Hence, the final value of k = 120 is printed.

3Predict Output — Snippets

Predict the output of the following snippet:

p = 100
for i in range(1, 5):
    p = p - 1
    print(p, end=' ')
Show SolutionHide Solution↓
99 98 97 96 

Let’s break down the code line by line:

1. p = 100: The variable p is assigned the value 100.

2. for i in range(1, 5):: The for loop runs for the values 1, 2, 3, 4. Therefore, the loop executes 4 times.

3. Iterations of the loop:

  • First Iteration: p = 100 - 1 = 99
  • Second Iteration: p = 99 - 1 = 98
  • Third Iteration: p = 98 - 1 = 97
  • Fourth Iteration: p = 97 - 1 = 96

4. print(p, end=' '): Displays the value of p after each iteration in the same line separated by spaces.

5. Therefore, the final output is: 99 98 97 96

4Predict Output — Snippets

Predict the output of the following snippet:

p = -99
while(p < 0):
    p = p + 9
    print(p, end=' ')
Show SolutionHide Solution↓
-90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0 

Let’s break down the code line by line:

1. p = -99: The variable p is assigned the value -99.

2. while(p < 0):: The while loop executes as long as the value of p is less than 0.

3. In each iteration, the statement p = p + 9 increases the value of p by 9 and print(p, end=' ') displays the updated value in the same line separated by spaces.

4. The values of p become -90, -81, -72, -63, -54, -45, -36, -27, -18, -9 and 0.

5. When p becomes 0, the condition p < 0 becomes False and the loop terminates.

6. Therefore, the output is -90 -81 -72 -63 -54 -45 -36 -27 -18 -9 0.

5Predict Output — Snippets

Predict the output of the following snippet:

for i in range(4):
    for j in range(i, -1, -1):
        print(j, end=' ')
    print()
Show SolutionHide Solution↓
0 
1 0 
2 1 0 
3 2 1 0 

The outer loop runs from 0 to 3. For each value of i, the inner loop starts from i and decreases up to 0 using the step value -1. Therefore, when i = 0, the output is 0; when i = 1, the output is 1 0; when i = 2, the output is 2 1 0; and when i = 3, the output is 3 2 1 0. The print() statement moves the output to the next line after each iteration of the outer loop.

6Predict Output — Snippets

Predict the output of the following snippet:

for x in range(1, 4):
    for y in range(1, 3):
        print(x*y, end=' ')
    print()
Show SolutionHide Solution↓
1 2 
2 4 
3 6 

The outer loop runs with values 1, 2, and 3. For each value of x, the inner loop runs with values 1 and 2. During each iteration, the product of x*y is printed. Thus, when x = 1, the outputs are 1 and 2; when x = 2, the outputs are 2 and 4; and when x = 3, the outputs are 3 and 6. The print() statement moves the cursor to the next line after each row is completed.

1Assertion and Reason based question

Assertion (A): Loop is a repetitive structure in which a statement or a set of statements are executed until the desired number of iterations is over.

Reason (R): The for loop is used to repeat the execution of a block of statements for a fixed number of times.

Based on the above assertion and reasoning, pick an appropriate statement from the options given below:

Show SolutionHide Solution↓

Both A and R are true and R is not the correct explanation of A.

Reason — A loop is a repetitive structure used to execute statements repeatedly until the required number of iterations are over. The for loop is one type of loop that is used when the number of iterations is fixed. Therefore, both statements are true, but the second statement does not explain the first statement.

2Assertion and Reason based question

Assertion (A): The range() function is a built-in function which is used with the for loop.

Reason (R): This function doesn't require any parameter. It is used in for loop to generate the series such as even and odd numbers, positive and negative numbers, etc.

Based on the above assertion and reasoning, pick an appropriate statement from the options given below:

Show SolutionHide Solution↓

A is true but R is false.

Reason — The range() function is a built-in function used with the for loop to generate a sequence of numbers. In the range() function, the start value and step value are optional because their default values are 0 and 1 respectively. However, the stop value is mandatory. Therefore, the first statement is true but the second statement is false.

1Application based question

A Python program that takes a list of integers and tests the user's ability to:

  • write a code in Python.
  • use for loops for iteration to iterate over a list.
  • apply conditional statements to check divisibility.
  • print the results based on the condition.

A list of integers [10, 15, 24, 30, 20, 45, 25, 60] is provided.

The program counts and prints all numbers from the list that are divisible by 3 as well as 5.

The expected output:

The numbers divisible by 3 as well as 5 in the list are:
15
30
45
60
Number of elements divisible by 3 as well as 5: 4

An outline of the program is given below to make the task easier.

Now, fill in the blanks to complete the code. The program is given below:

# a program to display numbers divisible by 3 and 5 in the list
c = 0
My_List = [10, 15, 24, 30, 20, 45, 25, 60]
print("Given List:", My_List)
print("The numbers divisible by 3 as well as 5 in the list are:")
for ........... in My_List:                    # line 1
    if(num%3 == 0 ......... num%5 == 0):       # line 2
        print(num)
        c = ...............                    # line 3
print("Number of elements divisible by 3 as well as 5: ", c)
Show SolutionHide Solution↓

(a) Line 1: num

(b) Line 2: and

(c) Line 3: c+1

The complete program is given below:

# a program to display numbers divisible by 3 and 5 in the list
c = 0
My_List = [10, 15, 24, 30, 20, 45, 25, 60]
print("Given List:", My_List)
print("The numbers divisible by 3 as well as 5 in the list are:")
for num in My_List:                    # line 1
    if(num%3 == 0 and num%5 == 0):     # line 2
        print(num)
        c = c+1                        # line 3
print("Number of elements divisible by 3 as well as 5: ", c)
1(a)Python Programs

Write Python code to display the first ten terms of the following series:

1, 4, 9, 16, ...............

Show SolutionHide Solution↓
for i in range(1, 11):
    print(i*i, end=" ")
1 4 9 16 25 36 49 64 81 100
1(b)Python Programs

Write Python code to display the first ten terms of the following series:

1, 2, 4, 7, 11, ...............

Show SolutionHide Solution↓
n = 1
for i in range(1, 11):
    print(n, end=" ")
    n = n + i
1 2 4 7 11 16 22 29 37 46 
1(c)Python Programs

Write Python code to display the first ten terms of the following series:

2, 5, 10, 17, ...............

Show SolutionHide Solution↓
n = 2
for i in range(1, 11):
    print(n, end=" ")
    n = n + (2*i + 1)
2 5 10 17 26 37 50 65 82 101 
2(a)Python Programs

Design Python code to find and display the sum of the following series:

s = 1 + 4 + 9 + ............... + 400

Show SolutionHide Solution↓
s = 0
for i in range(1, 21):
    s = s + i*i
print("Sum of the series =", s)
Sum of the series = 2870
2(b)Python Programs

Design Python code to find and display the sum of the following series:

s = (1*2) + (2*3) + ............... + (19*20)

Show SolutionHide Solution↓
s = 0
for i in range(1, 20):
    s = s + (i * (i + 1))
print("Sum of the series =", s)
Sum of the series = 2660
3(a)Python Programs

Write Python code to find the sum of the given series:

1 + (1+2) + (1+2+3) + ............... + (1+2+3+...............+n)

Show SolutionHide Solution↓
n = int(input("Enter the value of n: "))
s = 0

for i in range(1, n+1):
    t = 0
    for j in range(1, i+1):
        t = t + j
    s = s + t

print("Sum of the series =", s)
Enter the value of n: 6
Sum of the series = 56
3(b)Python Programs

Write Python code to find the sum of the given series:

1 + (1*2) + (1*2*3) + ............... + (1*2*3*...............*n)

Show SolutionHide Solution↓
n = int(input("Enter the value of n: "))
s = 0
p = 1

for i in range(1, n+1):
    p = p * i
    s = s + p

print("Sum of the series =", s)
Enter the value of n: 5
Sum of the series = 153
3(c)Python Programs

Write Python code to find the sum of the given series:

S = 1! + 2! + 3! + 4! + ............... + n!

[Hint: Factorial of 5! = 5*4*3*2*1]

Show SolutionHide Solution↓
n = int(input("Enter the value of n: "))
s = 0
f = 1

for i in range(1, n+1):
    f = f * i
    s = s + f

print("Sum of the series =", s)
Enter the value of n: 6
Sum of the series = 873
4Python Programs

Write a program to find and display the sum of any ten natural numbers.

Show SolutionHide Solution↓
s = 0
print("Enter any ten natural numbers:")
for i in range(10):
    n = int(input())
    s = s + n
print("Sum of ten natural numbers =", s)
Enter any ten natural numbers:
5
4
6
7
3
2
9
8
10
23
Sum of ten natural numbers = 77
5Python Programs

Write a program to input any 20 numbers (including positive and negative). The program displays the sum of all positive numbers and negative numbers separately.

Show SolutionHide Solution↓
ps = 0
ns = 0

for i in range(20):
    n = int(input("Enter a number: "))

    if(n > 0):
        ps = ps + n
    elif(n < 0):
        ns = ns + n

print("Sum of positive numbers =", ps)
print("Sum of negative numbers =", ns)
Enter a number: 10
Enter a number: -5
Enter a number: 6
Enter a number: 9
Enter a number: -23
Enter a number: -4
Enter a number: -6
Enter a number: -11
Enter a number: 22
Enter a number: 33
Enter a number: 44
Enter a number: 10
Enter a number: 12
Enter a number: 6
Enter a number: 7
Enter a number: 8
Enter a number: -9
Enter a number: -2
Enter a number: -3
Enter a number: 19
Sum of positive numbers = 186
Sum of negative numbers = -63
6Python Programs

Write a program to enter a number and display the sum of its digits.

Sample Input: 642

Output: 12 (6+4+2 = 12)

Show SolutionHide Solution↓
n = int(input("Enter a number: "))
s = 0

while(n != 0):
    d = n % 10
    s = s + d
    n = n // 10

print("Sum of digits =", s)
Enter a number: 642
Sum of digits = 12
7Python Programs

Write a Python code to check and display whether a number input by the user is a composite number or not.

[A number is said to composite, if it has one or more factors excluding 1 and the number itself. For example: 4, 6, 8, 9, ...............]

Show SolutionHide Solution↓
c = 0
n = int(input("Enter a number: "))
for a in range(1, n+1):
    if n % a == 0:
        c = c + 1

if c > 2:
    print(n, "is a composite number")
else:
    print(n, "is not a composite number")
Enter a number: 9
9 is a composite number

Enter a number: 13
13 is not a composite number
8Python Programs

Write a program to input 10 different numbers. Display the greatest and the smallest numbers among them.

Show SolutionHide Solution↓
n = int(input("Enter a number: "))
greatest = n
smallest = n

for i in range(1, 10):
    n = int(input("Enter a number: "))

    if(n > greatest):
        greatest = n

    if(n < smallest):
        smallest = n

print("Greatest number =", greatest)
print("Smallest number =", smallest)
Enter a number: 12
Enter a number: 56
Enter a number: 45
Enter a number: 78
Enter a number: 19
Enter a number: 20
Enter a number: 30
Enter a number: 41
Enter a number: 87
Enter a number: 90
Greatest number = 90
Smallest number = 12
9Python Programs

Write a program to input a number. Check and display whether it is a Niven Number or not. (A number is said to be Niven when it is divisible by the sum of its digits).

Sample Input: 126

Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.

Show SolutionHide Solution↓
n = int(input("Enter a number: "))
p = n
s = 0

while n > 0:
    d = n % 10
    s = s + d
    n = n // 10

if p % s == 0:
    print(p, "is a Niven Number")
else:
    print(p, "is not a Niven Number")
Enter a number: 126
126 is a Niven Number

Enter a number: 167
167 is not a Niven Number
10Python Programs

Write a program to input a number and display the new number after reversing the digits.

Sample Input: 467

Sample Output: 764

Show SolutionHide Solution↓
n = int(input("Enter a number: "))
r = 0

while n > 0:
    d = n % 10
    r = r * 10 + d
    n = n // 10

print("Reversed number:", r)
Enter a number: 467
Reversed number: 764
11Python Programs

Write a Python code to display the Mathematical Tables from 5 to 10. Each Mathematical Table should run for 10 iterations as shown in the given format:

Sample Output: Table of 5

5*1 = 5
5*2 = 10
...............
...............
5*10 = 50
Show SolutionHide Solution↓
for i in range(5, 11):
    print("Table of", i)

    for j in range(1, 11):
        print(i, "*", j, "=", i*j)

    print()
Table of 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Table of 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60

Table of 7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

Table of 8
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

Table of 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

Table of 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
12Python Programs

Write a Python code to accept any 20 numbers and display only those numbers which are prime.

[Hint: A number is said to be prime, if it is divisible by 1 and the number itself.]

Show SolutionHide Solution↓
for i in range(1, 21):
    n = int(input("Enter a number: "))
    
    if n > 1:
        c = 0
        
        for j in range(1, n + 1):
            if n % j == 0:
                c = c + 1
        
        if c == 2:
            print(n, "is a prime number")
Enter a number: 4
Enter a number: 5
5 is a prime number
Enter a number: 6
Enter a number: 7
7 is a prime number
Enter a number: 8
Enter a number: 9
Enter a number: 2
2 is a prime number
Enter a number: 3
3 is a prime number
Enter a number: 12
Enter a number: 14
Enter a number: 15
Enter a number: 16
Enter a number: 78
Enter a number: 91
Enter a number: 20
Enter a number: 17
17 is a prime number
Enter a number: 39
Enter a number: 41
41 is a prime number
Enter a number: 19
19 is a prime number
Enter a number: 29
29 is a prime number
13Python Programs

A number is said to be 'Happy number' if eventual sum of the square of its digits results in 1. Write a Python code to input a number and check whether it is a Happy Number or not.

Sample Input: 31

Sample Output: 31 = 32 + 12 = 10 = 12 + 0 = 1

Hence, it is a Happy Number

Show SolutionHide Solution↓
n = int(input("Enter a number: "))
p = n

while n > 9:
    s = 0
    while n > 0:
        d = n % 10
        s = s + d*d
        n = n // 10
    n = s

if n == 1:
    print(p, "is a Happy Number")
else:
    print(p, "is not a Happy Number")
Enter a number: 31
31 is a Happy Number

Enter a number: 12
12 is not a Happy Number
14(a)Python Programs

Write Python code to display the following pattern:

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Show SolutionHide Solution↓
for i in range(1, 6):
    
    for j in range(5, i - 1, -1):
        print(j, end=" ")
    
    print()
5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 
14(b)Python Programs

Write Python code to display the following pattern:

1 3 5 7 9
1 3 5 7
1 3 5
1 3
1
Show SolutionHide Solution↓
for i in range(5, 0, -1):
    
    for j in range(1, 2 * i, 2):
        print(j, end=" ")
    
    print()
1 3 5 7 9 
1 3 5 7 
1 3 5 
1 3 
1 
14(c)Python Programs

Write Python code to display the following pattern:

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Show SolutionHide Solution↓
for i in range(1, 6):
    
    for j in range(5, 5 - i, -1):
        print(j, end=" ")
    
    print()
5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1 
14(d)Python Programs

Write Python code to display the following pattern:

1
3 5
5 7 9
7 9 11 13
9 11 13 15 17
Show SolutionHide Solution↓
for i in range(1, 6):
    
    n = 2 * i - 1
    
    for j in range(1, i + 1):
        print(n, end=" ")
        n = n + 2
    
    print()
1 
3 5 
5 7 9 
7 9 11 13 
9 11 13 15 17 
14(e)Python Programs

Write Python code to display the following pattern:

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Show SolutionHide Solution↓
for i in range(1, 6):
    
    for j in range(i, 6):
        print(j, end=" ")
    
    print()
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 
14(f)Python Programs

Write Python code to display the following pattern:

9
9 7
9 7 5
9 7 5 3
9 7 5 3 1
Show SolutionHide Solution↓
for i in range(1, 6):
    
    for j in range(9, 9 - (2 * i), -2):
        print(j, end=" ")
    
    print()
9 
9 7 
9 7 5 
9 7 5 3 
9 7 5 3 1 
1Answer the following questions

What is a loop?

Show SolutionHide Solution↓

A loop is a repetitive control structure in which a statement or a set of statements are executed repeatedly until the given condition is false or the desired number of iterations are over.

2Answer the following questions

Explain for loop with an example.

Show SolutionHide Solution↓

The for loop is a fixed iterative looping construct in Python. In this looping system, the number of iterations is fixed. The loop starts with a given value of the control variable and executes the block of statements by updating the control variable unless the last limit of the loop is reached. This loop is preferred when the user is aware of the number of times the process is to be repeated.

Syntax:

for <variable> in range(<parameters>):
    <statement or a set of statements>

Example:

for i in range(1, 6):
    print(i, end=' ')

Output: 1 2 3 4 5

This code displays the numbers from 1 to 5 using a for loop.

3Answer the following questions

What is significance of range() function in a for loop.

Show SolutionHide Solution↓

The range() function is a built-in function in Python which is used with the for loop to generate a sequence of integer numbers. It plays a significant role in defining the iteration limit of the for loop.

The range() function takes three parameters — initial value, stop value and update. The for loop iterates over the sequence generated by range() and executes the loop body for each value in the sequence. This makes the for loop simple, concise and easy to control.

4Answer the following questions

What are the parameters needed to create a for loop?

Show SolutionHide Solution↓

The parameters needed to create a for loop are passed through the range() function. These are:

  1. Initial value — It is an optional parameter that defines the starting value of the loop. The default value is 0.
  2. Stop value — It is a mandatory parameter that indicates the end condition of the loop. The loop will execute up to stop value - 1.
  3. Update — It is an optional parameter used to increase or decrease the value of the control variable. The default update value is 1.
5Answer the following questions

Write down the general format of:

(a) for loop

(b) while loop

Show SolutionHide Solution↓

(a) General format of for loop:

for <variable> in range(<parameters>):
    <statement or a set of statements>

(b) General format of while loop:

while <conditional expression>:
    statement 1
    statement 2
    ...............
    <update expression>
6Answer the following questions

What is an infinite loop? Give an example.

Show SolutionHide Solution↓

An infinite loop or an endless loop is a looping construct in which the number of iterations will never come to an end. The set of statements within the loop block keeps executing infinitely because the given condition never becomes false.

Example:

a = 5
while(a > 0):
    print(a*a)
    a = a + 1

In the above code, the value of 'a' will always be positive. Hence, the condition (a > 0) will never become false, resulting in never-ending iterations of the loop. Press Ctrl+C keys together to break the running of such an endless loop.

7Answer the following questions

State one similarity and one difference between while and for loops.

Show SolutionHide Solution↓

Similarity: Both for loop and while loop are used to repeat the execution of a block of statements until a particular condition is satisfied.

Difference: The for loop is a fixed iterative loop in which the number of iterations is known in advance, whereas the while loop is an unfixed iterative loop in which the user may not be aware of the exact number of iterations that will take place during execution.

8Answer the following questions

Distinguish between step loop and continuous loops.

Show SolutionHide Solution↓

The differences between step loop and continuous loop are:

Continuous LoopStep Loop
The control variable is updated (increased or decreased) only by 1 (one) after each iteration.The control variable is updated by a given value (other than one) after each iteration.
The loop terminates when the control variable reaches the last limit.The loop terminates when the value of the control variable reaches or exceeds the last limit of the range.
Example: for n in range(1, 11):Example: for n in range(1, 20, 2):
9Answer the following questions

What do you mean by a nested loop?

Show SolutionHide Solution↓

A nested loop is defined as a loop programmed within another loop. It is used in such a way that the inner loop repeats for a given number of times for each iteration of the outer loop.

10Answer the following questions

When do you need to use a nested loop?

Show SolutionHide Solution↓

A nested loop is used when a loop has to be programmed within another loop to perform a specific task. In a nested loop, the inner loop repeats for a number of times for each iteration of the outer loop. Nested loops are widely used in pattern printing.

Press ↓ to reveal a solution and move to the next question. Back to Solutions for Class 9 ICSE Exploring Robotics & Artificial Intelligence.