aspose file tools*
The moose likes Mock Exam Errata and the fly likes Loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Loop" Watch "Loop" New topic
Author

Loop

neelam samnani
Greenhorn

Joined: Mar 12, 2001
Posts: 8
class Loop {
public static void main( String [] agrs) {
int x=0; int y=0;
outer: for (x=0; x<100;x++) {
middle: for (y=0;y<100;y++) {
System.out.println("x=" + x + "; y=" + y);
if (y==10) { <<<

>>> } } }
} //main
} //class
The question is which code must replace the <<>> to finish the outerloop?
continue middle;

a) break outer;
b) break middle;
c) continue outer;
d) none of these...
Pls. suggest me.What is the right answer?


Neelam
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
When you say continue the outer loop I'm assuming you mean complete the loop that has the "outer" label. I would answer this question by using the process of elimanation.
a) Incorrect. break outer; would break out of all of the nested loops and start the loop all over again. So the outer loop would never complete.
b) Incorrect. break middle; will cause the inner loop to break out of the loop and execute the code starting with the line that's labeled "middle".
c) Correct. continue outer will cause the code to continue with the next iteration of the loop and if this is placed here the outer loop would complete.
d)Incorrect. continue outer works.

When continue is used, the code leaves the loop that the continue keyword is in and continues with the next iteration of the loop if no label is supplied. other wise it continues with the next iteration of the loop that has the label it uses.
When break is used without a label it exits the loop that the break keyword is in and it executes the code after the end of the loop. If a label is used it does the same thing except it jumps to the line of code that has the label.
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
Hi there,
I think answer is "a)break outer" because when y reaches 10 "break outer will be executed which means it will come out of outer loop. So no more execution as there is no statement after loop is finished. It is not true to say "When break is used without a label it exits the loop that the break keyword is in and it executes the code after the end of the loop. If a label is used it does the same thing except it jumps to the line of code that has the label. Actually it will come out of this outer loop with execution of "break outer".


------------------
Veer
Sarang Gurao
Greenhorn

Joined: Mar 14, 2001
Posts: 13
Hello
the break outer statement terminates the loop with outer label
and comes out of the loop while continue outer terminates the
current iteration and start with the next iteration till the for loop conditional statement returns true.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
The answers are b) and c) (break middle and (continue outer);
Ronnie is right about a) - this will break out of the outer loop completely.
b) is correct because when y is 10, break middle; is called so therefore, it jumps back to the outer loop, thus incrementing x by 1. (so the middle loop goes through 10 iterations, then x is x + 1 - all the way up to 99).
c) is correct for the same reason as b
d) is not correct. (continue middle; seems to make an infinite loop).
Tejas Kansara
Ranch Hand

Joined: Dec 14, 2000
Posts: 38
Dear All
Undoubtly break outer is used.
Just check it out.
Regards
Tejas
Tejas Kansara
Ranch Hand

Joined: Dec 14, 2000
Posts: 38
Well, I apologise for my previous ans.
I wasn't clear with the question before but quite clear now.
The ans is
continue outer;
or
break middle
-Tejas
Tejas Kansara
Ranch Hand

Joined: Dec 14, 2000
Posts: 38
I apologise for the prev. answer.
Actually, I went thru the question again and corrected the answer and it is break middle or continue outer
Hope I am right this time.
Regards
Tejas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop