label loop program

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.






 Please advise. Any links, ideas, resources,sample code highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:
You should not expect to see "i3" there when you
when you have if(i>2){break outer; }
As soon as it encounter thos break and i is equal to 3 it breaks
out of the outer loop and you arrive already at "}//end of Br method" line
as there is nothing in you code after this outer loop.

Label in java is not a lable of particular location - it is a label
of the loop inside which it is sitting

 
0
 
for_yanCommented:
Seems kind of strange example.

One thing is that i will never be > 2 because i<3 in this loop

Another thing is I never saw these labeld breaks being used in the real code

This is how it would work:

             outer:
        for(int i=0;i <3;i ++){
              System.out.println("i"+i+"\n");
           //   outer://<==Point of this example
              if(i>1){
                      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

            System.out.println("i"  + i);
            System.out.println("sTILL IN OUTER");
          }//End of for

            System.out.println("out of OUTER");  

0
 
for_yanCommented:

It looks like label should be outside of the loop
out of which you want to jump out
and you need that it should be before loop
(execution will of course proceed after the loop,
but the label should appear before - otherwise
compiler says "undefined label").

I think the good practice is still not to use them
It is more common in case of nested loops and you need to jump
out of the outer loop under soe condition, is to maintain boolean
variable and when conditoion is satisfied , check it
and break out of the intermenidate cycle:
Something like that:

boolean stayInside = true;

for(int i=0; i<10; i++){

for(int j=0; j<10; j++){
if(j>5){
stayInside = false;
break;
}
}
if(!stayInside)break;
else {
//do something
}


}


0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gudii9Author Commented:
>>>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.
0
 
for_yanCommented:

Even though label  outer is placed above the loop it does not
mean that the program goes to excution to the location of
the outer label.
Position of the outer outside the outer loop just shows the program that
break outer should jum outside the outer loop.
When it jumps outside it does not return back to the beginning of
the loop it actually goes to the next operator after
the outer loop. It would be more logical I guess
to put it below the outer loop in frontb of the
next operator after the loop, but compiler does not allow that
because it needs to read the label before it reads
the lableled break operator. In fact what it labels is its location
outside the loop.

Please, explain again what you don't understand I'll try to explain more
 
0
 
gudii9Author Commented:
my question is i got output like



i0

j0
j1
j2
j3
i1

j0
j1
j2
j3
i2

j0
j1
j2
j3


i am thinking i should get like



i0

j0
j1
j2
j3
i1

j0
j1
j2
j3
i2

j0
j1
j2
j3
i3


with i3 at end. please advise
0
 
for_yanCommented:
Post the code exactly which you are excuting now.
there were too many diefferent codes pasted above.
0
 
Sathish David Kumar NConnect With a Mentor ArchitectCommented:
0
 
for_yanCommented:
Please, post the exact code which you are running once again in one post with the output - then we can consider
your code and output and find explanation - there were too many codes
posted above in this trail and they are not identical - so it is difficult to
analyze and understand the reasons for the output you get.
0
 
gudii9Author Commented:
code is

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");
    }
}



output i got is



i0

j0
j1
j2
j3
i1

j0
j1
j2
j3
i2

j0
j1
j2
j3
(here i was thinking i should get 'i3' also). please advise


0
 
for_yanCommented:
This is difficult to understand - you have only one  
System.out.println("i "+ i + " j "+j);

and this should always print both j and i, like
j1i1

Ho could you see this printout?
0
 
for_yanCommented:
maybe you are executing different program, not corresponding to this code?
0
 
for_yanCommented:

This is your code:
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");
    }
}

Open in new window


this is the output I got:

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

Open in new window

0
 
gudii9Author Commented:
sorry  i mean this program the one i posted in my first original question...



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                
}



output i got is



i0

j0
j1
j2
j3
i1

j0
j1
j2
j3
i2

j0
j1
j2
j3
(here i was thinking i should get 'i3' also). please advise
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.