0

I have the following program structure:

outerLoop:
for (i =0; i<x; i++) {
    if(check condition) {
       a = /* do something to calculate a */
       goto jump;
    } else {
        //do something else
    }
}

jump:
if (check condition) {
   //do something
   goto outerLoop;
}

So like seen above, I want to transfer the control from the if part of for loop to the if condition seen outside the loop. And I want to again jump to the for loop from the if statement. How do I do that? Is there a goto statement in Java?

millimoose
  • 39,073
  • 9
  • 82
  • 134
user93665
  • 5
  • 1
  • 4
  • 2
    you can use function calls. – Bill Jul 14 '13 at 19:16
  • Or just `break` if it's as simple as the code shown here. Labels and goto are not used in most modern programming languages. – confusopoly Jul 14 '13 at 19:17
  • 1
    Obligatory: [*A Case against the GO TO Statement*](http://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EWD215.html) – millimoose Jul 14 '13 at 19:17
  • @Bill Im sorry if this is a stupid question! But how do I use function calls here? – user93665 Jul 14 '13 at 19:17
  • It's hard to tell what you *should* do instead of `goto`, because it's not at all clear what you want to accomplish here. Mostly because your code isn't anywhere near valid Java, is indented wonkily, and contains nothing besides control flow. – millimoose Jul 14 '13 at 19:18
  • We can help in a better way if you can show us some real code. – Rohit Jain Jul 14 '13 at 19:20
  • 1
    @confusopoly Anytime you use `break` it's probably better to just split off the loop into a function and use `return` to make it explicit what the result of the "breakable" computation was, if any. – millimoose Jul 14 '13 at 19:21
  • @millimoose Good point. That makes it shorter and easier to grasp than the break, especially with a good function name. – confusopoly Jul 14 '13 at 19:27
  • I took a stab at fixing your code, but because pretty much none of your braces were balanced or anything indented in any sane way, it might not be how you intended it. – millimoose Jul 14 '13 at 19:34
  • 1
    @user93665 Sorry to be a late follower on this, but isn't that the first thing that Guru's teach you,"Don't make a sphagetti of your program by using JUMPs"...and I mean precisely in high-level code such as C/C++/Java? – ha9u63a7 Jul 14 '13 at 20:29
  • @hagubear In fact programs like what this would have been are specifically why gotos aren't in java – Richard Tingle Jul 14 '13 at 22:18
  • 1
    @hagubear Don't know about your "gurus" (ugh) but ours just went with never introducing the concept. There is one valid use of `goto` in C but that involves shared cleanup code before returning which would be clunky to express by adding another intermediate function. (Java has `try..finally` for this though.) – millimoose Jul 15 '13 at 12:44

5 Answers5

2

There is a way to jump using break and labels when you have multiple nested for loops.

see: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Example from link:

class BreakWithLabelDemo
{
    public static void main(String[] args)
    {
        int[][] arrayOfInts =
        {
            { 32,  87,   3,    589 },
            { 12,  1076, 2000, 8   },
            { 622, 127,  77,   955 }
        };

        int searchfor = 12;
        int i;
        int j = 0;
        boolean foundIt = false;

        search:
            for (i = 0; i < arrayOfInts.length; i++)
            {
                for (j = 0; j < arrayOfInts[i].length; j++)
                {
                    if (arrayOfInts[i][j] == searchfor)
                    {
                        foundIt = true;
                        break search;
                    }
                }
            }

        if (foundIt)
        {
            System.out.println("Found " + searchfor + " at " + i + ", " + j);
        }
        else
        {
            System.out.println(searchfor + " not in the array");
        }
    }
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Menelaos
  • 23,508
  • 18
  • 90
  • 155
2

Going against the other answers: don't use break either. I think what you're trying to accomplish here can be done this way:

int calculateA() {
    for (i = 0; i < x; i++) {
        if (/* a was found */) {
            return a;
        } else {
            // do something else
        }
    // What to do when `a` is not found is now explicit
    throw new Exception("cannot calculate a");
}

// OuterLoop should be an actual loop
int a = calculateA();
while (/* second condition */) {
    a = calculateA();
}
millimoose
  • 39,073
  • 9
  • 82
  • 134
1

Java does not support goto (although this is a reserved word). However java as other c-like languages supports break and continue. You need break here. It escapes from the loop.

In contrary to C java has break with label that is "almost" goto but limited. It is useful when you want to escape from several nested loops.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 3
    You can use `break` to break a block of code as well :) .. `label:{statement1; if(condition) break label; statement2;}` – Eng.Fouad Jul 14 '13 at 19:24
0

break or continue, according what you need to do.

see this post for the diffrence.

Community
  • 1
  • 1
user2141046
  • 862
  • 2
  • 7
  • 21
0

You can use function calls to jump between statements accompanied by break and continue. See if this helps:

for (i =0; i<x;i++){
if(check condition)
{
   /*do something
   calculate 'a'
   */
   jump();
   break; //use break if you want to exit the loop
}
else 
//do something else
}//method ends 


void jump()
{
   if(check condition)
   //do something
}
dejavu
  • 3,236
  • 7
  • 35
  • 60