This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Meaning of while (true)

 
Akin Millone
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chris webster
Bartender
Pie
Posts: 2367
31
Linux Oracle Postgres Database Python Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
HIH

Winston

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

 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Winston Gutkowski
Bartender
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2367
31
Linux Oracle Postgres Database Python Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic