wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Meaning of while (true) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Meaning of while (true)" Watch "Meaning of while (true)" New topic
Author

Meaning of while (true)

Akin Millone
Greenhorn

Joined: May 27, 2012
Posts: 4
Hello,
please i have a question about a line in chapter 2's guessgame code. The line is line 17: while(true)
I'd like to know... while WHAT is true?

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
It means while true is true. That is one way to code an endless loop.

NB: This thread was split from this one. Please don’t ask new questions unrelated to the subject of an old thread.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Akin Millone wrote:please i have a question about a line in chapter 2's guessgame code. The line is line 17: while(true)
I'd like to know... while WHAT is true?

It might be worth adding that the construct should generally be avoided, as it usually involves the loop containing break statements, which are also best kept to a minimum.

It IS sometimes used when all the conditions for ending the loop are difficult to put into a single expression, or when they only become apparent over a series of statements, but it can make path testing quite difficult.

In the context of a game, it's probably used as a substitute for
while(the game isn't over) { ...
in which case it may be better to put the body of the loop in a separate method that returns a boolean, and use something like:HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1624
    
  13

Winston Gutkowski wrote:
HIH

Winston

Just curious, but why is the "for" loop better than a "while" loop like this?



No more Blub for me, thank you, Vicar.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

chris webster wrote:Just curious, but why is the "for" loop better than a "while" loop like this?

Basically: scope. The 'keepGoing' variable is defined and used exactly where it's needed.

Some people may regard it as posteriorly-retentive, but I reckon it's a pretty good rule; which is why I generally prefer for loops to while's.

Winston
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's funny how different people develop different prejudices. Personally I hate extraneous flag variables -- the ones introduced just to avoid break statements, for example.

You could also write



[Jess in Action][AskingGoodQuestions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

Ernest Friedman-Hill wrote:It's funny how different people develop different prejudices. Personally I hate extraneous flag variables...

Absolutely.

My personal hate is empty loops, but ONLY because Java doesn't have a noop statement, which makes them too easy to miss.
If it did, I'd be with you completely.

I suppose a possible alternative is:
Winston
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1624
    
  13

Winston Gutkowski wrote:
chris webster wrote:Just curious, but why is the "for" loop better than a "while" loop like this?

Basically: scope. The 'keepGoing' variable is defined and used exactly where it's needed. Some people may regard it as posteriorly-retentive, but I reckon it's a pretty good rule; which is why I generally prefer for loops to while's.

Winston

Good point - that's what I suspected. I tend to prefer "for" when there is a limit on the number of iterations - either via a count or the no of elements in a collection - and "while" only for an unknown no of iterations (which is much less common anyway). But it's good to learn from my more experienced peers!
 
 
subject: Meaning of while (true)
 
Similar Threads
return
Clarify....Assertions.....
Clarify...while....
Cannot flush() using BufferedWriter or PrintWriter
Assertion Question...explain this please