title: Infinite loops in Java
body: <p>Look at the following infinite <code>while</code> loop in Java. It causes a compile-time error for the statement below it.</p>
  
  <pre><code>while(true) {
      System.out.println("inside while");
  }
  
  System.out.println("while terminated"); //Unreachable statement - compiler-error.
  </code></pre>
  
  <hr>
  
  <p>The following same infinite <code>while</code> loop, however works fine and doesn't issue any errors in which I just replaced the condition with a boolean variable.</p>
  
  <pre><code>boolean b=true;
  
  while(b) {
      System.out.println("inside while");
  }
  
  System.out.println("while terminated"); //No error here.
  </code></pre>
  
  <hr>
  
  <p>In the second case also, the statement after the loop is obviously unreachable because the boolean variable <code>b</code> is true still the compiler doesn't complain at all. Why?</p>
  
  <hr>
  
  <p><strong>Edit :</strong> The following version of <code>while</code> gets stuck into an infinite loop as obvious but issues no compiler errors for the statement below it even though the <code>if</code> condition within the loop is always <code>false</code> and consequently, the loop can never return and can be determined by the compiler at the compile-time itself.</p>
  
  <pre><code>while(true) {
  
      if(false) {
          break;
      }
  
      System.out.println("inside while");
  }
  
  System.out.println("while terminated"); //No error here.
  </code></pre>
  
  <hr>
  
  <pre><code>while(true) {
  
      if(false)  { //if true then also
          return;  //Replacing return with break fixes the following error.
      }
  
      System.out.println("inside while");
  }
  
  System.out.println("while terminated"); //Compiler-error - unreachable statement.
  </code></pre>
  
  <hr>
  
  <pre><code>while(true) {
  
      if(true) {
          System.out.println("inside if");
          return;
      }
  
      System.out.println("inside while"); //No error here.
  }
  
  System.out.println("while terminated"); //Compiler-error - unreachable statement.
  </code></pre>
  
  <hr>
  
  <p><strong>Edit :</strong> Same thing with <code>if</code> and <code>while</code>.</p>
  
  <pre><code>if(false) {
      System.out.println("inside if"); //No error here.
  }
  </code></pre>
  
  <hr>
  
  <pre><code>while(false) {
      System.out.println("inside while");
      // Compiler's complain - unreachable statement.
  }
  </code></pre>
  
  <hr>
  
  <pre><code>while(true) {
  
      if(true) {
          System.out.println("inside if");
          break;
      }
  
      System.out.println("inside while"); //No error here.
  }      
  </code></pre>
  
  <hr>
  
  <p>The following version of <code>while</code> also gets stuck into an infinite loop.</p>
  
  <pre><code>while(true) {
  
      try {
          System.out.println("inside while");
          return;   //Replacing return with break makes no difference here.
      } finally {
          continue;
      }
  }
  </code></pre>
  
  <p>This is because the <code>finally</code> block is always executed even though the <code>return</code> statement encounters before it within the <code>try</code> block itself.</p>