Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Trying to understand boolean and while loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • 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: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
  • 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: 12834
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic