23

I want to break a while loop of the format below which has an if statement. If that if statement is true, the while loop also must break. Any help would be appreciated.

while(something.hasnext()) {
   do something...
   if(contains something to process){
      do something
      break if condition and while loop
   }
}
vatz88
  • 2,422
  • 2
  • 14
  • 25
SuperCoder
  • 262
  • 1
  • 3
  • 13

3 Answers3

54

The break keyword does exactly that. Here is a contrived example:

public static void main(String[] args) {
  int i = 0;
  while (i++ < 10) {
    if (i == 5) break;
  }
  System.out.println(i); //prints 5
}

If you were actually using nested loops, you would be able to use labels.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • @Codelearn14 You keep saying that, but no matter how many times you say it it will still be false. – Mike B May 08 '14 at 19:48
  • @Codelearn14 Can you post code example which will prove what you are saying? Maybe condition of `if` is never fulfilled preventing `break` from being invoked. – Pshemo May 08 '14 at 19:50
  • Yeah i think the same too. My bad sorry and thanks for the help – SuperCoder May 08 '14 at 19:52
  • @Codelearn14 I have added a simple example that you can run to confirm the behaviour. – assylias May 08 '14 at 19:53
  • Yeah It breaks.My program scans a zipcode file which has all the US zipcodes,city names and matches it with another file that contains some cities. If it finds a match, insert that zipcode and city into mySQL database. But the problem is that once it matches about 30000 zipcodes to 500 cities in another file, it gets really slow afterwards. So i thought to use a break statement to break the loop once it finds the match and start matching again for next zipcode. But still no use. the program still lags. – SuperCoder May 08 '14 at 20:02
  • @Codelearn14 It's called the [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why don't you ask a (new) question detailing your actual problem, what you have done (including relevant code samples) and why it does not work as expected? – assylias May 08 '14 at 20:04
  • 3
    I really wish @Codelearn14 didn't delete their comments because this argument looks hilarious. – John R Perry Apr 23 '18 at 11:45
9

An "if" is not a loop. Just use the break inside the "if" and it will break out of the "while".

If you ever need to use genuine nested loops, Java has the concept of a labeled break. You can put a label before a loop, and then use the name of the label is the argument to break. It will break outside of the labeled loop.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
7
while(something.hasnext())
do something...
   if(contains something to process){
      do something...
      break;
   }
}

Just use the break statement;

For eg:this just prints "Breaking..."

while (true) {
     if (true) {
         System.out.println("Breaking...");
         break;
     }
     System.out.println("Did this print?");
}
rahul pasricha
  • 931
  • 1
  • 14
  • 35