why doesnt the compiler give an error when u haveCode:else
whats the difference or there is none?Code:else;
Printable View
why doesnt the compiler give an error when u haveCode:else
whats the difference or there is none?Code:else;
the compiler recognizes else as a keyword and expects either an opening { followed by one or more statement and then a closing } or else it assumes that the next statement is a single line to be controlled by the else. A statement is a line of code ending with a semicolon.
Therefore else by itself is incomplete and else followed immediately by a semicolon basically means do nothing.
Code:if(true)
doThis();
else
{
doThat();//statement controlled by else
}
if(true)
doThis();
else
doThat();//statement controlled by else
if(true)
doThis();
else; //do nothing, since the statement controlled by else is empty
this should be perfectly legal
It sees the semicolon as a blank line, so it does nothing for the else.Code:if(condition) {
do something
}
else
;
Kind of like
That does nothing forever.Code:while(1)
;
// or
while(1);
it is, but it's a completely different concept... that has to do with the way statements are structured, not the "empty else" as my programming teacher used to call it