CBSE Coding Class 8 Solution – Get Creative With Loops
CBSE Coding Class 8 Solution Student Handbook Chapter 2 Get Creative With Loops all Questions and Answers Solution by Coding Teacher. Net Ex. Here provided all Coding Solution for Class 8.
(1) Which type of loop can be used till a condition is met?
Answer: While
Reason: while loop executes a set of commands till the condition is true.
(2) Which type of loop can be used to iterate over a sequence?
Answer: For
Reason: For loop is needed for iterating over a sequence until a certain Condition is met.
(3) Which statement is used to break the loop?
Answer: Break
Reason: Break statement is used to terminate a loop early when a condition is met.
Standard Question
(1) What are the different types of loops?
Answer: The different types of loops are:
(A) While Loop – The While loop can execute a set of commands till the condition is true. While loop first test a specified conditional expression and when the condition is true, action is taken. While Loops are also called conditional loops.
Syntex: While loop for finding sum of numbers until 10.
x = 0
while x is not 10
x = x + 1
print(x)
(B) For Loop – A loop that has set number of times to run. For loop execute the statements pf program several times until a given condition is met. For loop is needed for iterating over a sequence.
Syntax : For Loop for finding square of a number –
numbers = [1, 3, 5, 7, 13]
sq = 0
for x in numbers
sq = x * x
print(sq)
(C) Nested Loop – A loop that occurs within another loop is called nested loop. Nested loops consist of outer loop within one or more inner loops. While loop can be used as outer loop and for loop can be used as inner loop or vice versa.
Syntex: Example of Nested For loop:
numbers = [1, 2, 3]
alphabets = [a, b, c]
fornum in numbers
for alphabet in alphabets
print (alphabet)
(2) What is exit criteria?
Answer: Exit criteria is one of the most important components while defining a loop. Exit criteria is a set of conditions that must exist before you can declare a program to be complete. Without an exit criterion, the program might enter in an infinite loop. Exit criteria can be different for different programs depends upon the requirement of the programs.
(3) What is exit criteria for While loop?
Answer: Exit criteria for while loop is that we must know in advance the numbers of times the program going to run while completing a specific task. The condition must exist before you declare a program or it will enter in a continuous indefinite loop.
(4) What is exit criteria for For loop?
Answer: A loop needs to have an exit criteria if the condition is not met then the loop continues for infinite amount of time. When creating a loop to print numbers from 1 to 1000, exit criteria is that loop should exit the block of code when the 1000th number is printed, else the program will enter an infinite loop.
(5) What are nested loops?
Answer: Nested loops is defined as the loop inside the loop. Nested loops can be used as per the requirement of the program. Any loop can be used inside any loop. For loop can be used inside a while loop, while loop can be used inside a do while loop and goes on.
Types of nested loops:
(1) Nested For Loop
(2) Nested While loop
(3) Nested do-while loop
For Example – Nested For Loop
numbers = [1, 2, 3]
alphabets = [a, b, c]
fornum in numbers
for alphabet in alphabets
print(alphabet)
Higher Order Thinking Skills (HOTS)
(1) Make 50 horses and 50 cats rain from the sky using for loop.
Answer:
(2) Make stone stairs of height 15 blocks and width of 1 block using while loop.
Answer:
Applied Project
(1) First Initialize higher and lower.
(2) Initialize a variable to hold value of hcf. I.e. 1 and a variable name smaller to store the value of either higher or lower
(3) Check which value among higher or lower is smaller and store in variable names smaller.
(4) Run a loop in the iteration of i between [1, smaller]
(5) Note down the highest number that divides both higher and lower.
(6) If i satisfies (higher % i == 0 and lower % i == 0) then new value of HCF is i
(7) Print value of HCF.
Python Program to Find HCF of two numbers :
higher = 48
lower = 84
hcf = 1
Smaller = 0
if higher > lower:
smaller = lower
else:
smaller = higher
for i in range(1, smaller):
if higher % i == 0 and lower % i == 0:
hcf = i
print(“Hcf of”, higher, “and”, lower, “is”, hcf)