Is an if statement ALWAYS followed by an else? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is an if statement ALWAYS followed by an else?

23rd Aug 2016, 3:35 AM
neteprPS
8 Answers
+ 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");
23rd Aug 2016, 6:42 AM
James
James - avatar
+ 1
no. it is only if you want to do something else you wrote "javaily" after the "if"
23rd Aug 2016, 10:22 AM
Seungyoon Lee
+ 1
not all the time. I've seen a couple "if" statements without "else" and works perfectly, I have done it with python too
29th Aug 2016, 10:48 PM
Ezekiel Oladejo
Ezekiel Oladejo - avatar
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 :)
23rd Aug 2016, 3:52 AM
Aquarius
Aquarius - avatar
0
oh really? thanks james. i probably would have kept going with that and think it was right. Thanks
23rd Aug 2016, 11:11 AM
Aquarius
Aquarius - avatar
0
cool. thanks guys!!
23rd Aug 2016, 1:21 PM
neteprPS
0
no
24th Aug 2016, 11:27 AM
shridhar metraskar
shridhar metraskar - avatar
0
no ,its not compulsory to follow an if statement with an else
15th Sep 2016, 2:23 PM
sanket thakuria
sanket thakuria - avatar