Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loop

 
neelam samnani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All
Undoubtly break outer is used.
Just check it out.
Regards
Tejas
 
Tejas Kansara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic