Answer To Questions - 08. Iterative Constructs in Java
Answer To Questions - 08. Iterative Constructs in Java
3. State one similarity and one difference between while and do…while loop. [2] – 2005
State the difference between while and do…while loop. [2] – 2018
Ans: Similarity: Both are iteration constructs.
Difference:
while loop do…while loop
(i) It is an entry-controlled loop. (i) It is an exit-controlled loop.
(ii) In a while loop, the condition is tested (ii) In a do…while loop, the condition is
before entering the loop body. tested after exiting from the loop body.
(iii) The do…while loop body gets executed
(iii) The while loop body gets executed, at least once, irrespective of the condition.
only if the condition is true.
4. State one similarity and one difference between while and for loop. [2] – 2009
Ans: Similarity:
(i) Both are used for looping.
(ii) Both are entry-controlled loops/pre-tested loops/top-tested loops.
(iii) test expression is evaluated at the beginning
Difference:
while loop for loop
(i) All its loop-control elements are (i) All its loop-control elements are
scattered about the program. gathered in one place (on the top of the
loop).
(ii) It is suitable in a situation where it is not (ii) It is suitable when we know in advance
known before-hand when the loop will how many times the loop will be executed.
terminate.
5. Explain the meaning of break and continue statements. [3] – 2005
Explain the function of each of the following with an example:
(i) break
(ii) continue [4] – 2008
Name two jump statements and their use. [2] – 2010
What is the difference between a break statement and a continue statement when they occur
in a loop? [2] – 2013
Ans: Jump statements are break, continue and return.
break: It enables a program to skip over part of the code. It terminates the smallest enclosing
while, do…while, for or switch statement. Execution resumes at the statement immediately
following the body of the terminated statement.
continue: It forces the next iteration of the loop to take place, skipping any code in between.
It skips the rest of the loop body statements and causes the next iteration of the loop.
return: It causes an immediate exit from the method and it is used to return a value to the
calling code.
===========================