Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Meaning of while (true)

 
Akin Millone
Greenhorn
Posts: 4
  • 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: 49776
69
  • 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: 10498
64
Eclipse IDE Hibernate Ubuntu
  • 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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • 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: 10498
64
Eclipse IDE Hibernate Ubuntu
  • 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: 24212
35
Chrome Eclipse IDE Mac OS X
  • 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: 10498
64
Eclipse IDE Hibernate Ubuntu
  • 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
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • 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!
 
Prasannakshi Grandhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did had the same question that is mentioned here...and if we use this while(true) loop, is there as a possibility that we end up in else statement in the same program.

Program



Thanks
 
Prasannakshi Grandhe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry..I understood the else statement now.. How lucky am to get the true statement in the first time that confused me..)

Thanks
Prasanna
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the else in line 36 is used when nobody has got the guess right. I would prefer to get rid of the break. You might wish to use a do loop because you know you will always want to start the loop off. Maybe you would do this sort of thing:-
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that I have increased the scope of nobodyWon (which you can reduce by wrapping the entire loop in {}), but I Have reduced the scope of the three pXisRight variables. You would need to remove any earlier declarations.

Please check how to use the code tags; I corrected them for you.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"while (true)" is a kludgy way of saying that the test that exits the loop is either going to be made within the loop (via a "break" statement, return, Exception, or whatever) or if all else fails, because the application is forcibly cancelled either from the OS or actually pulling the plug on the whole machine.

Some programming languages actually have a special syntax construct for that:

Java isn't one of them, though. Sometimes C/C++ developers define a "forever" macro, but Java doesn't do macros either.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic