aspose file tools*
The moose likes Beginning Java and the fly likes What does it mean when a conditional has only the keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does it mean when a conditional has only the keyword "true" for a condition?" Watch "What does it mean when a conditional has only the keyword "true" for a condition?" New topic
Author

What does it mean when a conditional has only the keyword "true" for a condition?

John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
I've seen some If statements with a condition that's nothing more than the keyword true.

What does this mean?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

That it will always execute the IF part.

Once we are able to look into the programmer's brain, we might know why it was done .


OCUP UML fundamental and ITIL foundation
youtube channel
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

You mean like this?

It means that the body of the if-statement is always executed. It's ofcourse unnecessary to write code like that. I don't know why anyone would want to write code like that. Can you ask the people who wrote that code why they did this?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

The only reason I've ever done that is while debugging. For distance, if you want to temporarily return half-way through a method, you can't just add a return statement because the compiler will complain about unreachable code. But you can put it in an if statement.

If I remember correctly, the reason the JLS allows this is to support a kind of conditional compilation - unlike while(true){...}, which is disallowed.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Jesper de Jong wrote:I don't know why anyone would want to write code like that. Can you ask the people who wrote that code why they did this?

Probably it might be from a OCJP training guide / question
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
Jesper de Jong wrote:You mean like this?

It means that the body of the if-statement is always executed. It's ofcourse unnecessary to write code like that. I don't know why anyone would want to write code like that. Can you ask the people who wrote that code why they did this?


I got this from a tutorial on making java based video games. There's a text file with characters that build a game map and this code parses each char into a tile image or sprite.

So the conditional just loops if it has just the "true" statement?

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Did it compile? No statements shall follow a while(true) in a block. In the below methods, the first one compiles but not the second.

John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
Yes. That excerpt from the program in the book did compile.

I got the source code from this website and I bought the book its based on.

http://www.brackeen.com/javagamebook/

I don't understand. Is it the consensus of this forum that just having a plain boolean value as a condition is bad practice? If so, why is this code written like this?

Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

John Quach wrote:I've seen some If statements with a condition that's nothing more than the keyword true....


You got me there. The actual code you posted doesn't contain an if (true).
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
while(true) is used here to loop till the input is not null. Note that a break statement is used to come out of the loop. Basically the loop condition is checked inside the loop block and not on the while condition expression.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2477
    
    7

John Jai wrote:while(true) is used here to loop till the input is not null. Note that a break statement is used to come out of the loop. Basically the loop condition is checked inside the loop block and not on the while condition expression.


Congratulations with your 1000th post, John.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I earlier wrote: No statements shall follow a while(true) in a block....

Which is wrong - a break statement that breaks the while loop can be used to write statements after the while. So below code compiles fine.



Thanks Jan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
That looks a clumsy way to exit a loop. If you are reading, you can read in a while loop.
You ought to be catching the exceptions from your reader.
You ought to close the reader in a finally block, which you can find if you search this forum.
You can set a while loop to read every line, but it is syntax you would never have guessedIs that straight from the tutorial? I hope you are not paying for it
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

Campbell Ritchie wrote:You can set a while loop to read every line, but it is syntax you would never have guessed


Yes, and that's because it is a convoluted way to avoid having to write a loop which is executed "n-and-a-half times". Such loops are normal in computing, but when one just designed a language whose loop constructs don't support them properly, one tends to repress that knowledge. And hence one produces that ugly bit of code you posted, which has to be explicitly taught to every new Java programmer.

Whereas if you write it as a proper n-and-a-half loop it looks like this:


Or if you still have a religious objection to the break statement, you bring in a flag to salute:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I prefer the use of !finished.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Paul Clapham wrote:Or if you still have a religious objection to the break statement, you bring in a flag to salute:

What about:?

No break statement, no flags, and no nasty while() clause.

Winston


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

Joined: Oct 14, 2005
Posts: 18129
    
    8

Winston Gutkowski wrote:What about:?

No break statement, no flags, and no nasty while() clause.

Winston


You will have noticed that you have duplicate code there. Not the best idea. In this case there's only one line of code to be run before we know whether we have to do the second half of the loop, so it isn't that bad. If you had two lines, you would be having second thoughts about duplicating them, and if you had more than two, you wouldn't do it.

However, yes, I have seen that style before, in fact it's pretty much the standard in RPG code where I work.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
I've done this, when testing the body of an if statement
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

Paul Clapham wrote:You will have noticed that you have duplicate code there. Not the best idea. In this case there's only one line of code to be run before we know whether we have to do the second half of the loop, so it isn't that bad. If you had two lines, you would be having second thoughts about duplicating them, and if you had more than two, you wouldn't do it.
However, yes, I have seen that style before, in fact it's pretty much the standard in RPG code where I work.

It's old Dijkstra style:If the "action that causes condition(s)" part (which, yes, is repeated) takes more than one line, you just put it in a method.

Hasn't failed me since 1979, when I first learned about it (oddly enough, as an alternative to COBOL's horrible 'READ...AT END GOTO...') .

Winston
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
Campbell Ritchie wrote:That looks a clumsy way to exit a loop. If you are reading, you can read in a while loop.
You ought to be catching the exceptions from your reader.
You ought to close the reader in a finally block, which you can find if you search this forum.
You can set a while loop to read every line, but it is syntax you would never have guessedIs that straight from the tutorial? I hope you are not paying for it


Yes it is. And no I did not pay for it.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7063
    
  16

John Quach wrote:
Campbell Ritchie wrote:Is that straight from the tutorial? I hope you are not paying for it
Yes it is. And no I did not pay for it.

Personally, I don't see anything particularly terrible with it; although I know that some people are religiously opposed to "assignment" expressions.
I actually use it quite a lot myself, because it has the great virtue of putting everything in one place at the top of the loop, although there is also this alternative:
for (String line = reader.readLine(); line != null; line = reader.readLine()) {...

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does it mean when a conditional has only the keyword "true" for a condition?
 
Similar Threads
What is Static ?
String doubt
What is Inheritance
What is strictfp?
Difference between identifier and keyword