• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exiting do..while loop  RSS feed

 
Paul LeBlanc
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a do..while loop with two conditions to exit the loop. See line 62. It exits the loop if the first condition is true:
but will not exit the loop if the second condition is true: . What am I doing wrong.




 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does this do?

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to stop and think for quite a while before I realized what plant1.getLife() != false actually meant. In general a shorter way of writing the expression



is



Remember that boolean values can only be true or false, so if it isn't false then it's true. So you could have written plant1.getLife() instead. But that looks kind of strange, because it isn't obviously a boolean value and therefore you can't tell right away what it means. It would have helped if you had followed the Java Beans rules for boolean attributes and called the method isAlive instead. Then you could have wriiten plant1.isAlive() and it would have been obvious to readers of your code what was meant.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul LeBlanc wrote:What am I doing wrong.

You've misunderstood how the do-while loop works.

I have a do..while loop with two conditions to exit the loop.

The do-while continues to execute if the entire loop condition is true.

The way || works, the whole expression is true if at least one of the expressions on either side of || is true.

See line 62. It exits the loop if the first condition is true:

So if this condition is true, the loop will NOT exit

but will not exit the loop if the second condition is true: .

Likewise with this condition. The loop will continue if this condition is true.

If you want the loop to terminate if only one or none of these are true, then you should use && instead of ||.

BTW, it would be less confusing if you renamed getLife to isAlive. Then you can just write
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it helps if you can read your code out loud. With suitable names you could say to yourself "Do the stuff inside the loop while the activity ID isn't Q or the plant is alive". It's a bit easier to realize that you needed "and" instead of "or" if you can do that.
 
Paul LeBlanc
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
If you want the loop to terminate if only one or none of these are true, then you should use && instead of ||.

BTW, it would be less confusing if you renamed getLife to isAlive. Then you can just write


Thanks to all for the feedback. The makes better sense and it's working the way as intended.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!