Assign1-classX
Assign1-classX
Question 1
What is 'for' loop? What are the parameters used in 'for' loop?
for loop is an entry-controlled loop. The following parameters are commonly used in a
for loop:
An update expression to modify the loop control variable after every iteration.
Body of the loop which consists of the statements that needs to be repeatedly executed.
Question 2
An entry-controlled loop checks the condition at the time of entry. Only if the condition
is true, the program control enters the body of the loop. for and while loops are entry-
controlled loops.
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 3
(a) do - while
do {
//loop-body
} while (condition);
(b) while loop
while (condition) {
//loop-body
}
Question 4
continue statement is used to unconditionally jump to the next iteration of the loop,
skipping the remaining statements of the current iteration.
Question 5
while do-while
Question 6
A loop which continues iterating indefinitely and never stops is termed as infinite loop.
Below is an example of infinite loop:
for (;;)
System.out.println("Infinite Loop");
Question 7
State one difference and one similarity between while loop 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