jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Trying to understand boolean and while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to understand boolean and while loop" Watch "Trying to understand boolean and while loop" New topic

Trying to understand boolean and while loop

Gerald.M Beiles

Joined: Nov 05, 2006
Posts: 4
The method is demonstrating the use of a boolean and a while loop. The method will search through a collection of notes and see if the entered string is found. The method is listed below

My main problem is with the boolean variable and the while loop. My understanding is that the while loop should only execute if the index is less than the size of the notes in the class and "found" variable is true. The first condition will be true but the second one doesnt evaluate to true so wouldn't the whole condition evaluate to false and cause the while loop to never execute. Thanks for any help you might be able to offer. I know it must be simple but I can't get my head around the problem.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think you missed a sneaky punctuation mark.

The exclamation point reads "not" out loud and reverses the following boolean. So !found reads "not found" and is true when found is false. It loops while not found and stops when found (is true)

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Your condition includes "!found". That "!" means "not"; so it's "while not found" -- i.e., "while found is false, do this..." so setting found to true stops the loop.

[Jess in Action][AskingGoodQuestions]
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Gerald -

Your loop condition is:

The ! symbol in front of found needs to be read as "not"; if "not found" is true, then "found" must be false.

So your loop should continue while (index < notes.size()) evaluates "true" and not found evaluates "true" (which means that found evaluates as false).

The negations can be tricky, especially when combined with other conditions.

Have fun!
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

OH, wait... I know... the "!" in front of...

ah, shoot. nevermind.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gerald.M Beiles

Joined: Nov 05, 2006
Posts: 4
Thanks everyone for the help. I drift in and out of trying to keep the ! not operator straight in my head. I'll print this out and keep it handy.

I agree. Here's the link: http://aspose.com/file-tools
subject: Trying to understand boolean and while loop
It's not a secret anymore!