29

New! Save questions or answers and organize your favorite content.
Learn more.

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's user avatar

Seth

43.3k 10 gold badges 85 silver badges 118 bronze badges

asked Jan 6, 2010 at 21:47

mrblah's user avatar

6

  • Why don't you try it out? That's bound to be faster that writing a question ...

    Jan 6, 2010 at 21:49

  • 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)

    Jan 6, 2010 at 21:51

  • Interesting as a puzzler but in practice I never use the do-while loop. Somewhat controversially, I never use continue either.

    Jan 6, 2010 at 22:05

  • Googling and finding this question was much faster than writing code... Good thing mrblah wrote this question instead of just trying it out!

    Mar 27, 2014 at 5:04

  • 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.

    Mar 31, 2015 at 20:42

7 Answers 7

answered Jan 6, 2010 at 21:55

helios's user avatar

2

  • Can I comment that this is a strange behaviour. Since do while loops are expected to do an action before evaluating a condition.

    Sep 25, 2021 at 2: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.

    Nov 18, 2021 at 14:50

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); }                              

answered Jan 6, 2010 at 21:55

Jamie McCrindle's user avatar

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.

answered Jan 6, 2010 at 21:51

Rich Adams's user avatar

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

answered Jan 6, 2010 at 21:52

alphazero's user avatar

0

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.

answered Jan 6, 2010 at 21:51

patros's user avatar

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.

answered Jan 6, 2010 at 22:02

OscarRyz's user avatar

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)

answered Jan 6, 2010 at 21:51

Andres's user avatar

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)

    Jan 6, 2010 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.

    Jan 6, 2010 at 22:23