• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

label loop program

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Br{
public static void main(String argv[]){
Br b = new Br();
b.amethod();
}
public void amethod(){
for(int i=0;i <3;i ++){
System.out.println("i"+i+"\n");
outer://<==Point of this example
if(i>2){
break outer;//<==Point of this example
}//End of if

for(int j=0; j <4 && i<3; j++){
System.out.println("j"+j);
}//End of for
}//End of for
}//end of Br method
}

I was trying above example from linkhttp://www.jchq.net/certkey/0202certkey.htm
got output like



i0

j0
j1
j2
j3
i1

j0
j1
j2
j3
i2

j0
j1
j2
j3



I was trying to understand the output.Where outer label used in this loop. When we say outer does it mean below outer label if loop or above outer label for loop. Do we need to see below label or above label. Is it suppoesed to be i>2 or i>=2.


same way could not undestand

public class LabLoop{
public static void main(String argv[]){
LabLoop ml = new LabLoop();
ml.amethod();
}
public void amethod(){
outer:
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
if(j>1)
//Try this with break instead of continue
continue outer;
System.out.println("i "+ i + " j "+j);
}
}//End of outer for
System.out.println("Continuing");
}
}
This version gives the following output

i 0 j 0
i 0 j 1
i 1 j 0
i 1 j 1
Continuing


If you were to substitute the continue command with break, the i counter would stop at zero as the processing of the outer loop would be abandoned instead of simply continuing to the next increment.
 
author & internet detective
Posts: 42169
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the label refers to the line right after it. (or later n the line if you don't put a line break there)

As far as i > 2 or i >= 2, what happens when you try each way?
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>>System.out.println("i"+i+"\n");

should print i3 finally i believe right.
>>>System.out.println("i"+i+"\n");
>>>outer://<==Point of this example
>>>if(i>2){
>>>break outer;//<==Point of this example


As the outer label is after above system.out.println statement. So evenouter breaks still i3 should print right. I am not clear why i3 did not printed as output here. please advise.
 
reply
    Bookmark Topic Watch Topic
  • New Topic