aspose file tools*
The moose likes Beginning Java and the fly likes Question RE: guessing game example in Head First Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question RE: guessing game example in Head First Java" Watch "Question RE: guessing game example in Head First Java" New topic
Author

Question RE: guessing game example in Head First Java

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
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

Joined: May 26, 2003
Posts: 30749
    
156

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
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

Joined: May 26, 2003
Posts: 30749
    
156

True is the expression it is evaluating. Needless to say, it evaluates to the boolean true.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Jul 15, 2009
Posts: 537

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().


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question RE: guessing game example in Head First Java