100% found this document useful (1 vote)
354 views

Answer To Questions - 08. Iterative Constructs in Java

Uploaded by

janudev0205
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
354 views

Answer To Questions - 08. Iterative Constructs in Java

Uploaded by

janudev0205
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit 8 – Iteration constructs in Java

1. State the difference between entry-controlled loop and exit-controlled loop.


[2] – 2011
Ans: Entry-controlled loop Exit-controlled loop
(i) In an entry-controlled loop, the loop (i) In exit-controlled loop, the loop
condition is tested before entering the loop condition is tested after exiting from the
body. loop body.
(ii) Only if the condition is true, the loop (ii) Loop body executed at least once,
body gets executed. irrespective of the condition.
(iii) Examples: for and while loops (iii) Example: do…while loop

2. Explain the term for loop with an example. [2] – 2005


Ans: The for loop is an iteration construct used to execute a set of statements repeatedly until a
certain condition is fulfilled.
Example:
for(int i=1 ; i<=10 ; i++)
System.out.println(i);
Firstly, initialization expression is executed i.e., int i=1. Then, the test expression is evaluated
i.e., i<=10. Since, the test-expression is true, the body of the loop i.e., System.out.println(i)
is executed. After executing the loop-body, the update expression i.e., i++ is executed. After
update expression is executed, the test-expression is again evaluated. If it is true, the sequence
is repeated.

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.

6. State the type of loop in the given program segment:


for(int i=5 ; i!=0 ; i –= 2)
System.out.println(i);
(a) finite
(b) infinite
(c) null
(d) fixed [1] – 2023
Ans: (b) infinite

7. What is meant by infinite loop? Give an example. [2] – 2008


What is an infinite loop? Write an infinite loop statement. [2] – 2014
What is an infinite loop? Give an example. [2] – 2020
Ans: A sequence of statements which repeats endlessly is called an infinite loop. It is an endless
loop. It never stops executing its body.
Example:
for( ; ; )
System.out.println(“Endless for loop”);

===========================

You might also like