title: Java: While loop does not exit
body: <p>Does any one have any idea why this while look ONLY exits IF theres a System.out.println()?</p>
  
  <p>The very same code doesn't work if I comment out the println()</p>
  
  <pre><code>do{
  
      System.out.println("");
  
      if(hasWon()){
          InOut.out(output() + "\nYou Won");
          System.exit(0);
      }
  } while (!hasWon()) 
  </code></pre>
  
  <p>The program is as follows</p>
  
  <pre><code>static final int gridSize = Integer.parseInt(InOut.in(1, "Enter grid size"));
  static Tile[][] board = new Tile[gridSize][gridSize];
  static int winCond = 1;
  static GuiFrame f = new GuiFrame(gridSize);
  static BtnPanel p = new BtnPanel(gridSize);
  static JButton[][] btn = new JButton[gridSize][gridSize];
  
  public static void main(String[] args) {
  
      //Creating objects
      for (int i = 0; i &lt; gridSize; i++) {
          for (int z = 0; z &lt; gridSize; z++) {
              board[i][z] = new Tile();
          }
      }
      GUI();
  
      while (!hasWon()) {
  
          System.out.println("");
  
          if(hasWon()){
              InOut.out(output() + "\nYou Won");
              System.exit(0);
          }
      }
  }
  
  public static boolean hasWon() {
      boolean hasWon = true;
  
      for (int i = 0; i &lt; gridSize; i++) {
          for (int z = 0; z &lt; gridSize; z++) {
              hasWon = hasWon &amp;&amp; (board[i][z].getStatus() == winCond);
          }
      }
  
      return hasWon;
  }
  
  public static String output() {
      String message = "";
  
      for (int i = 0; i &lt; gridSize; i++) {
          for (int z = 0; z &lt; gridSize; z++) {
              message += board[i][z].getStatus() + " ";
          }
          message += "\n";
      }
  
      return message;
  }
  
  public static void GUI() {
  
      for (int i = 0; i &lt; gridSize; i++) {
          for (int z = 0; z &lt; gridSize; z++) {
              String btnValue = "";
              btnValue += board[i][z].getStatus();
              btn[i][z] = new JButton();
              btn[i][z].setText(btnValue);
              btn[i][z].addActionListener(f);
              p.add(btn[i][z]);
          }
      }
  
      f.add(p, BorderLayout.CENTER);
  
  }
  
  public static void modifyGUI(int i, int z){
      btn[i][z].setText(String.valueOf(board[i][z].getStatus()));
  }
  </code></pre>
  
  <p>This is a puzzle game where the user clicks a tile and adjacent tiles change as well. However when the puzzle is completed, without a println() it does not show completion, with a println() it exits the loop and exits the program.</p>
  
  <p>To re-emphasize, everything works fine if theres a println() inside the while loop. When i comment it out it doesnt work.</p>