New course! Every coder should learn Generative AI!
Try a free lesson0
Why else is not indispensable when using if statements?
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!
0
o thanks