We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello!
I'm currently developing an interactive data visualization and I'm attempting to use an if statement with 8 conditions. After I added the last condition, the program didn't carry it out so I decided to println the variable that defined the condition. Upon adding this prinln() to the code, the program started crashing processing every time I run it. Do if statements have a limit on the amount of conditions they can carry out? Can I use nested if statements to solve this or will that not work either?
Thanks!!
Answers
println can cause crashes
you can use multiple ifs though
@Nasif --
The problem is more likely that some aspect of how a specific conditional term is being evaluated needs to be debugged. Consider this 14-condition if statement sketch, which works fine:
See related discussion of multiple conditions in Java and short circuiting:
Anything that evaluates to only true or false can be used.
Breaking the if statement into multiples is possible and may help.
Might also be clearer to assign the individual tests to boolean local variables and use those in the if()
Post the condition
yeah post your code. Maybe a typo
Use a function
I often just program a function with return type boolean to take care of it. This function must have a good name such as
boolean allAsteroidsAreDestroyed()
.Then a complicate if just becomes
In this function you are more free to work with multiple lines and tests:
Work with a buffer
To accumulate tests you can use a buffer. This allows you to split ifs up:
Another approach
I was once thinking about an idea but only theoretically.
Let's say you have a situation where you have many if-clauses and are confused. But you know that you have let's say 6 possible results of your tests like in a game of chess e.g.: moveWasIllegal, moveWasCastling, moveWasGivingCheck, moveWasGivingCheckMate, moveWasEnPassant, moveWasStandard ....
You could approach this problem from the results and enumerate the possible results and for loop over them and then test each case separately:
(The
final ints
must probably have global scope and are not only visible in the function.)Chrisir ;-)
(@Chrisir It is "Check" and "Checkmate")
(Thanks. I corrected this)
Thank you all very much! Upon troubleshooting it turns out the println() was the one giving me problems, once I moved it elsewhere it all worked again. Thanks for all the possibilities on using conditionals I really did learn a lot :)