PDA

View Full Version : The Or and if-else-if logic



wkt
July 23rd, 2009, 06:04 AM
Hi there

I'm wondering... How does the Or logic work?



if (1 or 2 or 3 or 4 or 5) {
doSomething()
}


How does it run the test? I mean if 1 is true, will it jump the test for 2, 3, 4 and 5 and carry out whatever inside the curly brackets?

What about this?



if (1) {
doSomething()
} else if (2) {
doSomething()
} else if (3) {
doSomething()
} else if (4) {
doSomething()
} else if (5) {
doSomething()
}


So if 1 is true, it won't carry out the test for 2, 3, 4 and 5. Is my understanding correct?

Thanks in advance.

Gnoll
July 23rd, 2009, 06:07 AM
or as in '||' ?

Yes I think your understanding is correct,
Gnoll

There is also Switch - Case, which can be easier to manage than a large if-else-if

wkt
July 23rd, 2009, 06:11 AM
What about performance?

p.s. so using the or logic, if 1 is true, will 2, 3, 4 and 5 be checked?

Charleh
July 23rd, 2009, 06:20 AM
Yeah I think in AS all the comparisons are checked before the or is evaluated - so in this case 1,2,3,4,5 will be checked. There's a language construct in VB which allows you to check one at a time (OrElse, and AndAlso), which means you can check if object references are set before you try to access member properties/methods.

You understanding sounds fine to me, I'd do a test though, you can create a function which modifies a variable and returns a boolean. If you put this at the end of your OR list, but make sure the first value in the list is the only one that needs to be evaluated for the statement to be true, if the variable was modified afterwards you know that the function was called.

BoppreH
July 23rd, 2009, 08:21 AM
The "||" and "&&" comparisons are Short-Circuited.
http://en.wikipedia.org/wiki/Short-circuit_evaluation

So, if 1 is true, 2, 3, 4 and 5 will not be evaluated. Same with "1 && 2 && 3 && 4 && 5". When it reachs the first one that is false, it'll drop everything after.


if (true && trace(1) && trace(2) && trace(3) && trace(4) && trace(5)) {
trace("Executing...")
}
Output:

1
Because "trace" does not return a value, thus it's false.


if (true || trace(1) || trace(2) || trace(3) || trace(4) || trace(5)) {
trace("Executing...")
}
Outputs only

Executing...
Because the first argument is already true, so there's no need to evaluate the others.

wkt
July 23rd, 2009, 08:29 AM
Hm... So there are 2 sets of arguments here. Charleh said that in Or logic, all comparisons will be checked. Boppre said that if the first one returns a true, the rest will be skipped.

Any 3rd, 4th, 5th opinions?

Charleh
July 23rd, 2009, 08:33 AM
Boppreh's probably talking truth about both methods || and 'or' - just some languages don't short circuit :P

Try it out in Flash with both :)

pradvan
July 23rd, 2009, 09:52 AM
Because the first argument is already true, so there's no need to evaluate the others.

Yup, he's right.

SparK_BR
July 27th, 2009, 12:23 AM
did you test it with AS2
using keyword 'or' and not '||' ?

just to check if there's a difference betwen AS2 and 3 about shot-circuiting

Krilnon
July 27th, 2009, 12:47 AM
There appears to be a difference:


if(true || trace(1)){
trace(2);
}

if(true or trace(3)){
trace(4);
}


…outputs "2, 3" (formatted differently, of course). Strangely the 4 never prints…