32

I have a do while that looks like:

User user = userDao.Get(1);

do
{
 // processing


 // get the next user
 //
 user = UserDao.GetNext(user.Id);

 if(user == null)
       continue;   // will this work?????????????
}
while ( user != null)

If it does work, its going to go to the top of the do statement, and user is null so things are going to break?

Maybe I should rework the loop to a while statement?

Seth
  • 45,033
  • 10
  • 85
  • 120
mrblah
  • 99,669
  • 140
  • 310
  • 420
  • 42
    Why don't you try it out? That's bound to be faster that writing a question ... – meriton Jan 06 '10 at 21:49
  • 2
    It seems pretty odd. You should simplify the code. What are you trying to do? getting the first not null user? Or processing all the not null users? When do you know there are not more users? (the do/while suggests that null indicates end of users... but the if..continue suggest there are null users in between... :S) – helios Jan 06 '10 at 21:51
  • meriton: I used to think the same thing, but sometimes these people don't have compilers on their machine at the moment (may or may not be the case here, though ...) – Noon Silk Jan 06 '10 at 22:04
  • 1
    Interesting as a puzzler but in practice I never use the do-while loop. Somewhat controversially, I never use continue either. – Michael Easter Jan 06 '10 at 22:05
  • 10
    Googling and finding this question was much faster than writing code... Good thing mrblah wrote this question instead of just trying it out! – antak Mar 27 '14 at 05:04
  • 3
    People don't seem to understand stackoverflow's idea. This question does not only help mrblah but probably hundreds of googlers. This might be a pathetic example right here, but meriton's arument is totally invalid. – phil294 Mar 31 '15 at 20:42

7 Answers7

53

The continue makes it jump to the evaluation at the botton so the program can evaluate if it has to continue with another iteration or exit. In this case it will exit.

This is the specification: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#6045

Such language questions you can search it in the Java Language Specification: http://java.sun.com/docs/books/jls/

helios
  • 13,574
  • 2
  • 45
  • 55
  • Can I comment that this is a strange behaviour. Since do while loops are expected to do an action before evaluating a condition. – user13947194 Sep 25 '21 at 02:59
  • Not really strange. "Control passes to the loop-continuation point of an iteration statement" - loop evaluates a condition, a part of which 'continue' turns out to be, executes continue, and passes control to the loop-continuation point, which is condition evaluation. Seems legitimate to me. – Oleksii Karnatskyi Nov 18 '21 at 14:50
5

This really wouldn't be the best way to write this code. If user is null, you'll get a NullPointerException when you try and get user.id the next time around. A better way to do this would be:

User user = UserDao.Get(1);
while(user != null) {
  // do something with the user
  user = UserDao.GetNext(user.id);
}
Jamie McCrindle
  • 9,114
  • 6
  • 43
  • 48
4

Yes, continue will work in a do..while loop.

You probably want to use break instead of continue to stop processing the users, or just remove the if null continue bit completely since the while loop will break out as soon as user is null anyway.

Rich Adams
  • 26,096
  • 4
  • 39
  • 62
3

Why are you testing user in two places? The while condition will terminate the loop when user is null, which is what you want.

alphazero
  • 27,094
  • 3
  • 30
  • 26
1

Short answer yes, continue (and break) work properly in do while loops.

As others have pointed out though, from the example it looks like you may be expecting the wrong behavior from continue.

patros
  • 7,719
  • 3
  • 28
  • 37
0

The continue will jump to the loop evaluation statement inside the while which looks redundant as your if and while are evaluating the same thing . I think it would be better to use a break or simply let the while condition do the work (your while condition is already checking it for you)

Andres
  • 3,324
  • 6
  • 27
  • 32
  • 2
    the `continue` says to evaluate the `while` condition without going through the rest of the loop body. It does certainly not plainly repeat the loop (-1) – xtofl Jan 06 '10 at 22:13
  • Yes you are right. I made a confusion with while{} where in this case it will go to the top of the loop and evaluate again. – Andres Jan 06 '10 at 22:23
0

Let's see:

$cat DoWhileContinue.java 
class DoWhileContinue {

    public static void main( String [] args ) {
        int i = 0;
        int y = 0;
        do {
            i++;
            if( i > 100 ) {
                continue;
            }
            y++;

        } while( i < 500  );
        System.out.printf("i=%d, y=%d %n", i, y );
    }
}
$javac DoWhileContinue.java 
$java DoWhileContinue
i=500, y=100 

Yes it does. In this sample you can see y value is 100 because the continue statement was executed and prevented the variable from being increased afterward.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569