title: Java: While Loop for entering username and password 3 times - Issue with break statement?
body: <p>I'm doing an assignment for class and want to create a loop that gives a user 3 opportunities to log in, entering the right username and password.</p>
  
  <ul>
  <li><p>When successful, giving the message "You are now logged in"</p></li>
  <li><p>When entered wrong 1-2 times, giving the message "wrong entry, try again (...)", then prompting to enter username and password again</p></li>
  <li><p>When entered wrong 3 times, giving the message "You have entered wrong three times (...)", then exit the system</p></li>
  </ul>
  
  <p>Here is the part of my code:</p>
  
  <pre><code>System.out.println("Enter password");
  String inputPassword = input.next();
  
  int count = 0;
  
  //create while loop, set loop continuation condition to count &lt; 3
  while (count &lt;= 2) {            
  
      if ((!inputUsername.equals(userName)) || (!inputPassword.equals(password))) {       
          System.out.println("Wrong entry. try again: Enter username");
          inputUsername = input.next();
  
          System.out.println("Enter password");
          inputPassword = input.next();
      }   
      else
          System.out.println("You are now logged in");    
  
      count++;
  
      break;  
  }
  
  if (count &gt; 2)
   System.out.println("You have enterede wrong three times. Please try again in a few hours");
   System.exit(0);
  </code></pre>
  
  <p>Any idea why the system exits after 2 failed attempts without giving me the error message "You have entered wrong three times (...)"?</p>
  
  <p>I believe the problem lies in the "break" after incrementing the count, but not sure.</p>
  
  <p>Without this break, in case of successful entry, the console displays "you are now logged in" 3 times followed by the error message "You have entered wrong three times (...)" - which is why I put it.</p>