Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out of a for loop in Java [closed]

In my code I have a for loop that iterates through a method of code until it meets the for condition.

Is there anyway to break out of this for loop?

So if we look at the code below, what if we want to break out of this for loop when we get to "15"?

public class Test {     public static void main(String args[]) {        for(int x = 10; x < 20; x = x+1) {          System.out.print("value of x : " + x );          System.out.print("\n");       }    } }  Outputs:  value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 

I've tried the following to no avail:

public class Test {     public static void main(String args[]) {       boolean breakLoop = false;       while (!breakLoop) {           for(int x = 10; x < 20; x = x+1) {              System.out.print("value of x : " + x );              System.out.print("\n");           if (x = 15) {               breakLoop = true;           }           }       }    } } 

And I've tried a loop:

public class Test {     public static void main(String args[]) {       breakLoop:           for(int x = 10; x < 20; x = x+1) {              System.out.print("value of x : " + x );              System.out.print("\n");              if (x = 15) {                  break breakLoop;              }       }    } } 

The only way I can achieve what I want to is by breaking out of a for loop, I cannot subsitute it for a while, do, if etc statement.

Edit:

This was provided only as an example, this isn't the code I'm trying to get it implemented into. I have now solved the problem by placing multiple IF statements after where each loop initilizes. Before it would onlu jump out of one part of the loop due to lack of breaks;

like image 360
silverzx Avatar asked Mar 07 '13 15:03

silverzx


People also ask

Can you break out of a for loop Java?

Java Break Statement with Labeled For LoopWe can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.

Can you use break to exit a for loop?

Using break to exit a LoopUsing break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.

Does break only exit one loop Java?

the break only exits loop B , so the code will loop forever. This code will not loop forever, because the break explicitly leaves loop A . Fortunately, this same logic works for continue .


1 Answers

break; is what you need to break out of any looping statement like for, while or do-while.

In your case, its going to be like this:-

for(int x = 10; x < 20; x++) {          // The below condition can be present before or after your sysouts, depending on your needs.          if(x == 15){              break; // A unlabeled break is enough. You don't need a labeled break here.          }          System.out.print("value of x : " + x );          System.out.print("\n"); } 
like image 74
Rahul Avatar answered Nov 15 '22 13:11

Rahul