44

I have been using Java for quite some time, yet my education in loops is somewhat lacking. I know how to create every loop that exists in java and break out of the loops as well. However, I've recently thought about this:

Say I have two nested loops. Could I break out of both loops using just one break statement?

Here is what I have so far.

int points = 0;
int goal = 100;
while (goal <= 100) {
    for (int i = 0; i < goal; i++) {
        if (points > 50) {
           break; // For loop ends, but the while loop does not
        }
        // I know I could put a 'break' statement here and end
        // the while loop, but I want to do it using just
        // one 'break' statement.
        points += i;
    }
}

Is there a way to achieve this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fireshadow52
  • 6,298
  • 2
  • 30
  • 46

6 Answers6

86

In Java you can use a label to specify which loop to break/continue:

mainLoop:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break mainLoop;
      }
      points += i;
   }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sirbrialliance
  • 3,612
  • 1
  • 25
  • 15
  • But isn't goto (in this case, pseudo-goto) evil? – zw324 Jul 10 '11 at 00:11
  • @ZiyaoWei: IMHO `goto` is not "pure-evil". Just use it with caution and only when you really need. – Grzegorz Szpetkowski Jul 10 '11 at 00:13
  • 1
    @Grzegorz I do miss the good old days that I could use goto without someone pointing my back;) – zw324 Jul 10 '11 at 00:14
  • 2
    @ziyao as long as the code is clear about what happens (the destination is near the goto) it's fine and IMHO cleaner – ratchet freak Jul 10 '11 at 00:14
  • 2
    http://xkcd.com/292/, that said, PHP allows you to "break 2;" the Java version is more clean and maintainable IMHO. – sirbrialliance Jul 10 '11 at 00:14
  • 2
    Yeah, while I usually prefer encapsuling the inner loop in an extra function because that's usually clearer, using a goto is surely clearer than using an extra boolean variable and setting that. – Voo Jul 10 '11 at 00:40
  • 1
    I cant believe I hadn't known this! Thank you so much for the new lesson! – Kyle Aug 10 '15 at 12:17
20

Yes, you can write break with label e.g.:

int points = 0;
int goal = 100;
someLabel:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break someLabel;
      }
   points += i;
   }
}
// you are going here after break someLabel;
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
6

There are many ways to skin this cat. Here's one:

int points = 0;
int goal = 100;
boolean finished = false;
while (goal <= 100 && !finished) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         finished = true;
         break;
      }
   points += i;
   }
}

Update: Wow, did not know about breaking with labels. That seems like a better solution.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
1

Elementary, dear Watson ...

int points = 0;
int goal = 100;

while (goal <= 100) {
  for (int i = 0; i < goal; i++) {
    if (points > 50) {
      goal++;
      break;
    }
  points += i;
  }
}

or

int points = 0;
int goalim = goal = 100;

while (goal <= goalim) {
  for (int i = 0; i < goal; i++) {
    if (points > 50) {
      goal = goalim + 1;
      break;
    }
  points += i;
  }
}
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
0

You can reset the loop control variables.

int points = 0;
int goal = 100;
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         i = goal = 101;
      }
   points += i;
   }
}
Dipto_Das
  • 53
  • 11
0

You shouldn't use labels in objective language. You need to rewrite the for/while condition.

So your code should look like:

int points = 0;
int goal = 100;
while (goal <= 100 && points <= 50) {
    for (int i = 0; i < goal && points <= 50; i++) {
        points += i;
    }
}

// Now 'points' is 55
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131