0
Is an if statement ALWAYS followed by an else?
8 Réponses
+ 4
Not always, no. Only when you have multiple pieces of code to run. Order goes if, else if, else btw Aquarius. Just else is a "if conditions aren't met, do this" statement that doesn't allow other ifs to follow. An example of just if would be:
int x = 10;
if(x = 5)
System.out println("x is 10");
Without the else, nothing else is done, but the code works just fine. Doing else would be the equivalent of saying if x = anything else, do this.
else
System.out.println(" x is not 5");
else if is used if you want another option.
else if(x = 10)
System.out println("x is 10");
+ 1
no. it is only if you want to do something else you wrote "javaily" after the "if"
+ 1
not all the time. I've seen a couple "if" statements without "else" and works perfectly, I have done it with python too
0
As in order, it would make sense to put them in order. if not youll be confused. so you do:
if
else
else if
But im not quite sure if im right or not. im just going with my common sense. so please correct me if im wrong in any way :)
0
oh really? thanks james. i probably would have kept going with that and think it was right. Thanks
0
cool. thanks guys!!
0
no
0
no ,its not compulsory to follow an if statement with an else