3

what does the operators evaluate on instructions?

like:

var flag:Boolean=true;
flag && trace("1") && trace("2") && trace("3");

output: 1 2 3

var flag:Boolean=true;
flag && trace("1") || trace("2") || trace("3");

output 1

The or operator breaks the sentence under idk what circumstances...

Edit: ok, now i see how the sentence works with the operators, but is any instruction that doesn't have a return value valuated as true?

And it appears u can't evaluate any instruction u want, like:

private function any():void{
true && return;
}

that will throw a compilation error.

Edit: In this case, it behaves different to the second example:

true && one() || two() || three();

the functions

private function one():void{
    trace("1");
}
private function two():void{
    trace("2");
}
private function three():void{
    trace("3");
}

output: 1 2 3

Edit: Assigning values:

var a:int;
(a=1) || trace("1");
(a=2) && trace("2");

output: 2

Edit: || Returns expression1 if it is true or can be converted to true, and expression2 otherwise

&& "Returns expression1 if it is false or can be converted to false, and expression2 otherwise. Examples of values that can be converted to false are 0, NaN, null, and undefined".

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#logical_OR

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#logical_AND

2 Answers 2

6

This is called short-circuit evaluation. Basically the and/or expression evaluation is stopped when its result can be determimed for sure.

For example when evaluating and (&&) evaluation can be stopped at the moment first false is found. false && true && true && true... is always false, no matter how many true-values you add to end. Same goes for or and true-value.

Sign up to request clarification or add additional context in comments.

1 Comment

oh, that really makes sense, and explains why the sentence breaks, but any instruction will always return true??
5

ActionScript 3 is an ECMAScript variant, which means it's based off the same standard as JavaScript.

Because of this && and || are not similar to other C-style languages.

a || b returns a if a is truthy, otherwise it returns b.

a && b returns a if a is falsey, otherwise it returns b.

This makes || essentially equivalent to a ? a : b, and && essentially equivalent to !a ? a : b.

They also have a short-circuit evaluation mechanism. If the first argument is to be returned, the second argument is not executed.

4 Comments

could u provide a little example on this: "|| essentially equivalent to a ? a : b"
@wvxvw, if you're using strongly typed objects, you'll receive strongly typed values. If you use dynamically typed objects (i.e. var a:*, or simply var a) you'll receive the dynamically typed value.
@wvxvw, simply put, you're mistaken. || is often used for its coalescing behavior when setting objects. a = a || {} works because a will be truthy if set, and falsey if undefined. I will update my answer with links to the documentation, please take some time to review it.
@wvxvw, no problem. I was beginning to worry that I'd gotten it wrong for AS3, as it's been quite a while since I've actually written any flash code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.