• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Trying to understand boolean and while loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OH, wait... I know... the "!" in front of...

ah, shoot. nevermind.

:-)
 
Gerald.M Beiles
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic