Alex Rivera | Logout

Does `continue` jump to the top of a `do while`?

Asked 2010-01-06T21:47:27.913
35

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 jump to the top of the loop?
}
while( user != null )

Will it go to the top of the do statement, and user == null so things are going to break?

Maybe I should rework the loop to a while statement?

Edit
Report

1 Answer

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.

answered 2010-01-06T22:02:12.347

Your Answer