Why else is not indispensable when using if statements? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why else is not indispensable when using if statements?

23rd Jul 2017, 7:40 PM
Augusto Josue Sanchez Leiva
Augusto Josue Sanchez Leiva - avatar
2 Answers
+ 4
Else will run if everything is false in the if statement 'hierarchy'. (Meaning the if/ else if statements). What if I don't want to run something if all those conditions are false? That's why it's not mandatory. Sometimes I may want to only do one operation under a certain condition, without doing a different operation if that condition is not met. Example/ int x = -5; // Make x positive if(x < 0) { x = -x; } // That's all I wanted to check!
23rd Jul 2017, 7:44 PM
Rrestoring faith
Rrestoring faith - avatar
0
o thanks
23rd Jul 2017, 7:46 PM
Augusto Josue Sanchez Leiva
Augusto Josue Sanchez Leiva - avatar