Iterative Constructs
for , while, do..while
Multiple Choice Questions
ICSE | IX
CodeLogical
Answer the Following Questions
Question 1
What do you understand by iterative process? How can it be resolved by using a loop?
Iterative process means repeating a set of actions a certain number of times to perform
some task. Loops in programming languages like Java enable us to repeat a single statement or
a set of statements as long as the desired condition remains true.
Question 2
Explain the loop with an example.
for loop is an entry-controlled loop. Below is an example of for loop:
for (int i = 1; i <= 12; i++) {
int a = 2 * i;
[Link]("2 x " + i + "\t= " + a);
}
This for loop prints the table of 2 till 12. int i = 1 is the initialization part of the for loop, it is
executed only once when the loop gets executed for the first time. i <= 12 is the
condition part of the for loop, it is executed before the start of each iteration. Loop
iterates as long as this condition remains true. Once it becomes false, execution of the
loop is stopped. i++ is the update part of the for loop. It is executed at the end of each
iteration.
Question 3
Name the different types of loop statements.
1. for
2. while
3. do-while
Question 4
What are the parameters needed to create a for loop?
The following parameters are commonly used in a for loop:
1. An initial value for the loop control variable.
2. A condition—loop will iterate as long as this condition remains true.
3. An update expression to modify the loop control variable after every iteration.
4. Body of the loop which consists of the statements that needs to be repeatedly executed.
Question 5
Define the following with their constructs:
(a) Entry controlled loop
An entry-controlled loop checks the condition at the time of entry. Only if the condition is true,
[[Link]] 1/5
the program control enters the body of the loop. for and while loops are entry-controlled loops.
(b) Exit controlled loop
An exit-controlled loop checks the condition after executing its body. If the condition is true, loop
will perform the next iteration otherwise program control will move out of the loop. do-while loop
is an exit-controlled loop.
Question 6
Write down the general format of:
(a) for loop
for (initialization; condition; update) {
//loop-body
}
(b) do - while
do {
//loop-body
} while (condition);
(c) while loop
while (condition) {
//loop-body
}
Question 7
What is the purpose of using:
(a) break statement
break statement is used to unconditionally jump out of the loop
(b) continue statement
continue statement is used to unconditionally jump to the next iteration of the loop,
skipping the remaining statements of the current iteration.
Question 8
What do you understand by inter-conversion of loops?
We can convert the repetitive logic written using one type of loop into any of the other 2
types. For example, if some repetitive logic is coded using a for loop, we can convert that for
loop into while or do-while loop. This is termed inter-conversion of loops.
[[Link]] 2/5
Question 9
What are the different ways to inter-convert the loops? Name them.
1. for loop to while loop
2. for loop to do-while loop
3. do-while loop to while loop
4. do-while loop to for loop
5. while loop to do-while loop
6. while loop to for loop
Question 10
Define the following:
(a) Finite loop
A loop which iterates for a finite number of iterations is termed as a finite loop.
(b) Delay loop
A loop which is used to pause the execution of the program for some finite amount of time is
termed as Delay loop. Delay loops have an empty loop body.
(c) Infinite loop
A loop which continues iterating indefinitely and never stops is termed as infinite loop.
(d) Null loop
A loop which has an empty loop body is termed as a null loop.
Question 11
Distinguish between:
(a) for and while loop
1. for loop is a suitable choice when we know the number of iterations beforehand. while
loop is helpful in situations where numbers of iterations is not known.
2. Omitting the condition in for loop will lead to an infinite loop whereas if condition is not
provided in while loop, it will cause a compilation error.
(b) while and do-while loop
1. while is an entry-controlled loop whereas do-while is an exit-controlled loop
2. while loop is helpful in situations where numbers of iterations is not known. do-while is
suitable when we need to display a menu to the user.
Question 12
State one difference and one similarity between while and do-while loop
Similarity — Both while and do-while are suitable in situations where numbers of iterations is not
known.
Difference — while is an entry-controlled loop whereas do-while is an exit-controlled loop
[[Link]] 3/5
Question 13
State one similarity and one difference between while and for loop.
Similarity — Both for and while are entry-controlled loops
Difference — for loop is a suitable choice when we know the number of iterations
beforehand. while loop is helpful in situations where numbers of iterations is not known.
Question 14
Give two differences between Step loop and Continuous loop.
In the Continuous loop, the loop control variable is incremented or decremented by 1 in
each iteration whereas in the Step loop the loop control variable is incremented or decremented
by more than 1 in each iteration.
[[Link]] 4/5