Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does continue work in a do while?

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?

like image 580
mrblah Avatar asked Jan 06 '10 21:01

mrblah


People also ask

Does Continue statement work in do while loop?

Java continue statement is used for all type of loops but it is generally used in for, while, and do-while loops.

Can you use continue in a while loop Java?

Java continue statement is used to skip the current iteration of a loop. Continue statement in java can be used with for , while and do-while loop.

Does continue check while condition?

continue does not skip the check while(false) but simply ignores the rest of the code within the brackets.

Does continue work in while loop Python?

The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.


2 Answers

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/

like image 194
helios Avatar answered Sep 18 '22 21:09

helios


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); } 
like image 38
Jamie McCrindle Avatar answered Sep 22 '22 21:09

Jamie McCrindle