if() {
}else {
if (IsAlternateRow=='true')
IsAlternateRow = 'false';
else
IsAlternateRow = 'true';
}
Can I place an if and else statement inside of another else statement?
Note: the question got re-tagged as JavaScript after this answer was posted (it was originally about Java, so this answer is about Java).
In general, it is fine to place an if
and an else
inside an else
clause.
There are, however, several issues with your code. IsAlternateRow=='true'
isn't syntactically valid.
.equals()
;IsAlternateRow = !IsAlternateRow
instead of the entire nested if
.Yes, placing an if
inside an else
is perfectly acceptable practice but in most cases use of else if
is clearer and cleaner. E.g.
if (test) {
// Do something
} else if (otherTest) {
// Do something else
} else {
// Do a third thing
}
infact this is short-hand for
if (test) {
// Do something
} else {
if (otherTest) {
// Do something else
} else {
// Do a third thing
}
}
and the two should compile to almost identical programs in most situations.
Your code example is not very clear and will not compile correctly, clearer sample code may help us to help you out.
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