title: Breaking out of a for loop in Java
body: <p>In my code I have a for loop that iterates through a method of code until it meets the for condition.</p>
  
  <p>Is there anyway to break out of this for loop?</p>
  
  <p>So if we look at the code below, what if we want to break out of this for loop when we get to "15"?</p>
  
  <pre><code>public class Test {
  
     public static void main(String args[]) {
  
        for(int x = 10; x &lt; 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
  </code></pre>
  
  <p>I've tried the following to no avail:</p>
  
  <pre><code>public class Test {
  
     public static void main(String args[]) {
        boolean breakLoop = false;
        while (!breakLoop) {
            for(int x = 10; x &lt; 20; x = x+1) {
               System.out.print("value of x : " + x );
               System.out.print("\n");
            if (x = 15) {
                breakLoop = true;
            }
            }
        }
     }
  }
  </code></pre>
  
  <p>And I've tried a loop:</p>
  
  <pre><code>public class Test {
  
     public static void main(String args[]) {
        breakLoop:
            for(int x = 10; x &lt; 20; x = x+1) {
               System.out.print("value of x : " + x );
               System.out.print("\n");
               if (x = 15) {
                   break breakLoop;
               }
        }
     }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Edit:</p>
  
  <p>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;</p>