Output from a for loop in Java

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

Anyway, to break out of the for loop?

So, if we look at the code below, what if we want to break out of the 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 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 tried the 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 is to exit the for loop, I can’t substitute it for a long time, do it if, etc.

Edit:

This was provided as an example only, this is not the code I'm trying to implement. I solved the problem by putting several IF statements after each loop is initialized. Before he leaves one part of the loop due to the absence of interruptions,

+46
java loops for-loop break
Mar 07 '13 at 15:33
source share
5 answers

break; - this is what you need to break out of any loop statement such as for , while or do-while .

In your case, it will be as follows: -

 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"); } 
+119
Mar 07 '13 at 15:34
source share

You can use:

 for (int x = 0; x < 10; x++) { if (x == 5) { // If x is 5, then break it. break; } } 
+16
Mar 07 '13 at 15:35
source share

If for some reason you do not want to use the break statement (if you think this will upset your read flow the next time you read your program, for example), you can try the following:

 boolean test = true; for (int i = 0; i < 1220 && test; i++) { System.out.println(i); if (i == 20) { test = false; } } 

The second argument to the for loop is a logic test. If the test result is correct, the cycle will stop. You can use more than just a simple math test if you want. Otherwise, a simple breakthrough will also do the trick, as others have said:

 for (int i = 0; i < 1220 ; i++) { System.out.println(i); if (i == 20) { break; } } 
+14
Mar 07 '13 at 16:11
source share
 public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { if(x==15) break; System.out.print("value of x : " + x ); System.out.print("\n"); } } } 
+3
Mar 07 '13 at 15:36
source share

What about

 for(int k=0;k<10;k=k+2) { if(k==2) { break; } System.out.println(k); } 

another path marked as loop

 myloop: for (int i=0; i < 5; i++) { for (int j=0; j < 5; j++) { if (i * j > 6) { System.out.println("Breaking"); break myloop; } System.out.println(i + " " + j); } } 

for a better explanation you can check here

+2
Mar 07 '13 at 15:36
source share



All Articles