If there is an option of using & operator in if condition statements then why are nested if still in trend? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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.

15th Sep 2020, 3:19 PM
B Geetansh
B Geetansh - avatar
2 ответов
+ 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.
15th Sep 2020, 3:37 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
I totally agree with your point. There are always two sides of a coin.Nestet ifs are really useful here. Thanks a lot
15th Sep 2020, 3:40 PM
B Geetansh
B Geetansh - avatar