- Home /
If \ Else how does the program reads it?
ookkay.. I have one questions about if else.. I had a problem with a piece of code and when i added "else if", intead of just "else" it started working okay. So i was wondering what happens if i have && in the if statement and just "else"...
Let's say that i have this condition
if (objectA.position.x == 0.0 && objectB.position.y == 10.0)
{
... do some actions
}
else
{
... stop doing the actions
}
When i have && in hte "if" statement and only "else", does Unity reads it as:
else if (objectA.position.x != 0.0 && objectB.position.y != 10.0)
or
else if (objectA.position.x != 0.0 || objectB.position.y != 10.0)
on the else statement, if only one is false and the other true, will the else be executed, or it will continue with the first statement to be true? Or if some of the 2 statements are not true, then both if and else will be false and none will be executed?
I know that it's better in this case to add some statement in the else if, but i was curious how it works..
thanks :) , intead of just
Answer by Hoeloe · Sep 15, 2013 at 05:28 PM
The else
keyword is used as a performance optimisation, which allows some code to execute given a previous if statement didn't. Basically, the else
block will only be executed if the corresponding if
statement wasn't. So, if you have:
if(a && b)
//run code
else
//run more code
it is logically equivalent to:
if(a && b)
//run code
if(!a || !b)
//run more code
(assuming the values of a and b are not modified in the first if statement)
because !a || !b
is the condition under which the if statement is not executed. The benefit of the else statement over this second option, though, is that the execution is faster, because the code just needs to check if it ran the previous condition, rather than re-evaluating the expression. Also, else if
is logically identical to a standalone else block if an if statement inside - the if
is just part of the block, so when you write:
else if(a) {...}
you can think of it as:
else
{
if(a){...}
}
Meaning that the conditional will not be evaluated if the corresponding if statement was found to be true - again, this is a very useful performance optimisation.
Got it, thanks. It was little bit confusing for me, but when i see my code again, now i know why it was't working as expected with just "else"..
Thanks for the optimisation hints..
Answer by YoungDeveloper · Sep 15, 2013 at 05:31 PM
In the first condition, you are basically checking if(something is true AND something is true), if one of them will not be true, whole if statement won't be launched. Or operator ||, will be launched if least one statement is true.
if(1 == 1 && 1==2) {//wont be launched, because both must be true ( 1 isn't 2)}
if(1 == 1 || 1==2) {//will be launched, because at least one of these guys are true}
Else, only launches if the previous "if" statement wasn't successful, so if the "if" is true, next else will be ignored.
Now for the else if, if lets say we have 5 "else if" statements,
if(2 == 1){//wont be launched}
else if(3 == 2){//wont be launched}
else if(5 ==5 ) {//will be launched AND all next "else if" will be ignored till next "if"}
else if(1 == 1) {//will be ignored and not run, because one of the previous "else if" was true }
Your answer
Follow this Question
Related Questions
How to finish my else if statement? 2 Answers
How to make an if statement with two conditions? 1 Answer
if else statement doesnt work 1 Answer
|| inside if statement not working. (noob) 3 Answers
Check renderer.enabled statement from another object 3 Answers