• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Meaning of while (true)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76876
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 26294
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Dinner will be steamed monkey heads with a side of tiny ads.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic