1

I need to cleanly break out of a while loop(null check) and go to the next iteration of the outer for loop.

I have tried putting

for(Product: product:ListofProducts){
 while(null!=product.getDate){
    if(product.getDate>specifiedDate){
        doOnething()
    }
    else{
        doAnotherThing()
    }
    continue;
}

if the product date is not null and it does onething() or anotherthing() , then I want to move onto the next iteration of the for loop

6
  • set product.getDate() to null Commented May 16, 2019 at 9:57
  • use the "break" command Commented May 16, 2019 at 9:59
  • you can label your for loop nameofloop: for(Product: product:ListofProducts){..} and then continue nameofloop; Commented May 16, 2019 at 10:00
  • You are continuing the while loop in every case, is that desired? I don't really get the necessarity of a while loop in this case, anyway... Why do you use it? Commented May 16, 2019 at 10:00
  • 4
    You can use if instead of while, Is that the expectation is different? Commented May 16, 2019 at 10:02

2 Answers 2

4

There are a few ways.

You can break from the inner loop:

for(...) {
    while(...) {
       ... 
       if(condition) {
          break;
       }
       ...
    }
 }

This will leave the inner loop and the outer loop will continue.

Or you can label the outer loop, and use continue with the name. By default continue and break apply to the innermost loop, but using a name overrides that.

someName: for(...) {
    while(...) {
       ... 
       if(condition) {
          continue someName;
       }
       ...
    }
 }

Or, you can usually achieve it without break or continue:

for(...) {
    boolean done = false;
    while(... && !done) {
       ... 
       if(condition) {
          done = true;
       }
    }
 }

Some people advise avoiding break and continue for the same reason they advise avoiding return in the middle of a routine. Having more than one exit point for a routine is an opportunity for confusing the reader.

However, that can be mitigated by ensuring the routine is short. The problem is where your exit points get lost in long blocks of code.

Sign up to request clarification or add additional context in comments.

2 Comments

I always find the adding of an extra variable just to avoid the use of break more confusing than the break itself.
@aSlug I find that the longer I program, the less often I find I need to do any of these -- but I can't quite put my finger on why that is.
1
for(Product: product:ListofProducts){
 boolean done = false;
 while(null!=product.getDate && !done){
    if(product.getDate>specifiedDate){
        doOnething();
        done = true;
    }
    else{
        doAnotherThing();
        done = true;
    }
    continue;
}

1 Comment

The breaking condition in your example is always fulfilled. You are either not entering the inner loop or leaving it right after the first iteration. Your answer replicates the flaw of the original code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.