Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Conditional Statements  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a limit on the number of statements that can be used in the body of a Conditional Statement?
Thanks,
Landon
 
paul wheaton
Trailboss
Posts: 22553
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing that what you are asking is in the code fragment
if ( i > 0 )
{
// how many java statements can go here
}
If this is your question, I think the official answer is "unlimited", but I think if you work with other people, they might hurt you if put a lot in there - just because it's considered hard to read.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul....You read my mind. I'll keep the number of mys statements ina Conditional limited for readability, I was just curious about the limits.
Thanks for your help,
Landon
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two answers: Probably not, and Yes.
The first: There is likely no theoretical limit to the number of ORs and ANDs that you can have in a conditional statement (I am not aware of any). Any limit that does exist would be so large that you shouldn't have to worry about reaching it.
The second: There is a practical limit - readibility. Someone, somewhere, sometime will have to read your code. If you have fifteen lines of ORs and ANDs in your if statement, that someone might not be able to figure our what in the world is going on there. (And that someone might be you.)

What might be better would be:

This has the benefit of being a little bit more readable (at least to me ), but it has the much larger benefit of being more easily debugged. If you rin into some problem, you can put printlns after each isTrue = isTrue && ... and see exactly where your logic changes to false.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each method is allocated space for a frame and a call stack as defined by the implementer of the virtual machine. For most of these, including Sun's implementation, the answer would be "some very large number that should be more you than you'll ever need." In very early implementations I remember the number of statements permitted in any method being limited to 64k.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!