• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question RE: guessing game example in Head First Java

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying out the guessing game example in Ch. 2 of Head First Java. I've copied it and saved it (I like to do this, maybe to help get a feel for what's going on). I've saved it all on one file called GuessGame: Here's the code :


This is the entire program -- I have them saved as separate classes, so it compiled and ran fine. What I'd like to know is what does the while (true) condition mean in line 22? I know it has something to do with the booleans, but I'm not sure how to interpret the statement. What is going on that causes it to execute the statement inside the while loop?

 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Laurenzano wrote: What I'd like to know is what does the while (true) condition mean in line 22?


Normally when you have a loop, you write a condition inside the parens that evaluates to true or false which Java uses to determine whether to execute the loop another time. This example, always evaluates to true so Java always goes on to another loop execution.

The "break" in the loop is so this doesn't go on forever. When "break" is encountered, Java continues execution after the loop.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I asked my question correctly --

I meant to ask what does true refer to, or what condition is being evaluated that is either true or false? Is it evaluating the values of the p1isRight - p3isRight variables? or is it something else, because there are no other boolean variables/values in the file.



 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True is the expression it is evaluating. Needless to say, it evaluates to the boolean true.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true is just what it is, true. It is a boolean expression that always evaluates to true. It refers to nothing except itself.

Ok two almost identical answers. Jeanne beat me to it
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually these sort of things are mostly used in threads. If you want a thread to look at or get a value endlessly. While always requires a boolean value. SO inside a while, all expressions should evaluate to a boolean i.e in other words expressions are references to a boolean value in a loop. This may not sound good but its just hypothetical. So while(true) is a way of telling, run as much as you want like a never ending loop. Only way to get out of the loop is break, return and System.exit().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic