Discussion:
Just for Fun: Nested Loops and Goto in Java
Paul Z. Wu
2014-03-31 17:02:23 UTC
Permalink
OK, you don't need to tell me that I should avoid 'goto'....   I just had a nested loop which I didn't want to repeat some code or refactor it into some other ways to do it (sometimes, just getting too lazy :-).    Here came to the 'goto implementation'.  Check the comment 'why not zero' and please explain it to me.  Just for fun. Oh,  yes, please don't use 'goto'!


 


 public class MyGoto {
   
    public static void main(String s[])    {
       
        boolean g = true;
        gotoLabel: // not used, just for illustration.
        for (int m = 0; m < 4; m++) {
            outerloop:
            for (int k = 0; k < 3; k++) {
                if (g) {
                    if (k * m == 4) {
                        m = -1; // why not zero?
                        g = false;
                        System.out.println("break here");
                        break outerloop;  // Here effectively goto 'gotoLabel'.
                    }
                    System.out.println(m + "  " + k);
                } else {
                    System.out.println(m + "  " + k);
                }
            }
               //Dealing with the data....
            }
        }
}

Output:

0  0
0  1
0  2
1  0
1  1
1  2
2  0
2  1
break here
0  0
0  1
0  2
1  0
1  1
1  2
2  0
2  1
2  2
3  0
3  1
3  2

 
Paul Z. Wu
 
http://www.elookinto.com
Will McLean
2014-03-31 17:47:49 UTC
Permalink
break outerloop; doesn't exactly do a goto.

It terminates the labeled statement (the inner for) transferring control to the line after it
which in this case is the hidden m++ from the outer for.



________________________________
From: Paul Z. Wu <zwu_net-/***@public.gmane.org>
To: "seajug-***@public.gmane.org" <seajug-***@public.gmane.org>
Sent: Monday, March 31, 2014 10:02 AM
Subject: [seajug] Just for Fun: Nested Loops and Goto in Java



 
OK, you don't need to tell me that I should avoid 'goto'....   I just had a nested loop which I didn't want to repeat some code or refactor it into some other ways to do it (sometimes, just getting too lazy :-).    Here came to the 'goto implementation'.  Check the comment 'why not zero' and please explain it to me.  Just for fun. Oh,  yes, please don't use 'goto'!


 


 public class MyGoto {
   
    public static void main(String s[])    {
       
        boolean g = true;
        gotoLabel: // not used, just for illustration.
        for (int m = 0; m < 4; m++) {
            outerloop:
            for (int k = 0; k < 3; k++) {
                if
(g) {
                    if (k * m == 4) {
                        m = -1; // why not zero?
                        g = false;
                        System.out.println("break here");
                        break outerloop;  // Here effectively goto 'gotoLabel'.
                   
}
                    System.out.println(m + "  " + k);
                } else {
                    System.out.println(m + "  " + k);
                }
            }
               //Dealing with the data....
            }
        }
}

Output:

0  0
0  1
0  2
1  0
1  1
1  2
2  0
2  1
break
here
0  0
0  1
0  2
1  0
1  1
1  2
2  0
2  1
2  2
3  0
3  1
3  2

 
Paul Z. Wu
 
http://www.elookinto.com
P.Hill
2014-04-01 01:12:45 UTC
Permalink
Post by Will McLean
break outerloop; doesn't exactly do a goto.
It terminates the labeled statement (the inner for) transferring
control to the line after it
which in this case is the hidden m++ from the outer for.
To terminate the illustrated _inner_ loop all he needs is a break <no
label> to exit the loop.
I guess the code at
// dealing with the data
IS what he wants next, so no gotos or even labels required.
I often implement nested loops with two ways out as a multiple return
from a method just for the loops - findRightSpotToDealWithData()
I'm not sure that would work in this case.

-Paul
Paul Z. Wu
2014-04-01 17:00:40 UTC
Permalink
Not exactly.  I use one alg to process a 2-dimensional array -- however when some conditions happen during the processing, the alg is not applicable anymore, I would like to fall back to another alg.  The fallback alg has to start from the very beginning of the 2-dimensional array as the first alg. So it is not  just simple to break the inner loop. 
 
 
Paul Z. Wu
 
http://www.elookinto.com


________________________________
From: P.Hill <parehill1-***@public.gmane.org>
To: seajug-***@public.gmane.org
Sent: Monday, March 31, 2014 6:12 PM
Subject: Re: [seajug] Just for Fun: Nested Loops and Goto in Java



 
On 3/31/2014 10:47 AM, Will McLean wrote:

 
Post by Will McLean
break outerloop; doesn't exactly do a goto.
It terminates the labeled statement (the inner for) transferring control to the line after it
which in this case is the hidden m++ from the outer for.
To terminate the illustrated _inner_ loop all he needs is a break <no label> to exit the loop.
I guess the code at
// dealing with the data
 IS what he wants next, so no gotos or even labels required.
I often implement nested loops with two ways out as a multiple
return from a method just for the loops -
findRightSpotToDealWithData()
I'm not sure that would work in this case.

-Paul
P.Hill
2014-04-01 19:37:45 UTC
Permalink
OK, a labeled break IS what you can use. In your original code I
believe that would be
break gotoLabel;
because the label "outerLoop" labels the inner loop.

A label on the outer loop is exactly what is shown in Java Tutorial on
Oracle.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

-Paul
Not exactly. I use one alg to process a 2-dimensional array --
however when some conditions happen during the processing, the alg is
not applicable anymore, I would like to fall back to another alg. The
fallback alg has to start from the very beginning of the 2-dimensional
array as the first alg. So it is not just simple to break the inner
loop.
Paul Z. Wu
Loading...