I have these long statements that I will refer to as x,y etc. here. My conditional statements' structure goes like this:
if(x || y || z || q){
if(x)
do someth
else if (y)
do something
if(z)
do something
else if(q)
do something
}
else
do smthing
Is there a better, shorter way to write this thing? Thanks
I don't see a big problem with how you write it now. I do recommend using curly braces even for single statement if-blocks. This will help you avoid mistakes in case you have to add more code lines later (and might forget to add the curly braces then). I find it more readable as well. The code would look like this then:
if (x || y || z || q) {
if (x) {
do something
} else if (y) {
do something
}
if (z) {
do something
} else if (q) {
do something
}
} else {
do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With