Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question RE: guessing game example in Head First Java

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • 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?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 105
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True is the expression it is evaluating. Needless to say, it evaluates to the boolean true.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • 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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • 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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic