• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen some If statements with a condition that's nothing more than the keyword true.

What does this mean?
 
Bartender
Posts: 2658
19
Netbeans IDE C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2658
19
Netbeans IDE C++ Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2658
19
Netbeans IDE C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 70248
282
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 25812
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 70248
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer the use of !finished.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Paul Clapham
Marshal
Posts: 25812
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done this, when testing the body of an if statement
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic