+ 3
If there is an option of using & operator in if condition statements then why are nested if still in trend?
I mean they are not bad but they make the code look bulky amd boring.
2 Respostas
+ 7
There are times when nested ifs are simply the better option to get a piece of code to reflect the logic you have in mind, perhaps by allow you to execute code when the first condition is true, prior to the evaluation of the second condition.
if (a) {
// init, do something first
if (b) {
// proceed with option 1
}
else ...
}
in which case doing init before checking condition 'a' may be a waste of resource, and doing multiple if statements with && operators may seem clunky:
if (a && b) {
// init, do option 1
}
if (a && !b) {
// init ...
}
Observe that you are now writing init code twice, which is unnecessary if you opt for nested ifs.
+ 1
I totally agree with your point.
There are always two sides of a coin.Nestet ifs are really useful here.
Thanks a lot