Saturday, 4 June 2016

how to break out of nested loops java



break;

By using break we can come out from single loop.

But if we want to come out from all nested loop then we shoule use lables for acheiving this concept.



Example:

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;

No comments:

Post a Comment