注册 | 登录

(九) Thunder分布式RPC框架

nepxion 分享于

up vote 6 down vote favorite

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
java if-statement while-loop break share | improve this question edited Feb 20 at 6:46 Vatsal 571 2 12 asked May 8 '14 at 19:37 Coder2017 47 1 1 9 2   You said the answer in your question! –  Josef E. May 8 '14 at 19:40      break if loop something do while loop confused me. I thought there are two loops. –  Braj May 8 '14 at 19:42      you can't say if(true){break;}. –  Braj May 8 '14 at 20:09 add a comment  | 

3 Answers 3

active oldest votes up vote 21 down vote accepted

if is not a loop. Simply call break;.

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.

share | improve this answer edited May 8 '14 at 19:50 answered May 8 '14 at 19:38 assylias 188k 28 380 522      @Codelearn14 You keep saying that, but no matter how many times you say it it will still be false. –  Mike B May 8 '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 8 '14 at 19:50      Yeah i think the same too. My bad sorry and thanks for the help –  Coder2017 May 8 '14 at 19:52      @Codelearn14 I have added a simple example that you can run to confirm the behaviour. –  assylias May 8 '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. –  Coder2017 May 8 '14 at 20:02  |  show 2 more comments up vote 1 down vote

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.

share | improve this answer answered May 8 '14 at 19:39 Ernest Friedman-Hill 65.1k 7 96 148 add a comment  |  up vote 1 down vote
do something...
   if(contains something to process){
      do something...

Just use the break statement;

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

while (true) {
     if (true) {
     System.out.println("Did this print?");
share | improve this answer edited May 8 '14 at 19:53 answered May 8 '14 at 19:40 rahul pasricha 730 11 31      @rahul Please answer –  Braj May 8 '14 at 19:46 add a comment  | 

Your Answer

  draft saved draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name Email

Post as a guest

Name Email discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged java if-statement while-loop break or ask your own question.

Thunder(QQ 群 471164539)发布在淘宝代码基地 1. 介绍治理中心是基于Nepxion Swing Repository组件,Java Desktop版的服务治理系统,计划用基于Ebay










您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱