Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 Math issue

Explorer ,
Jul 16, 2014 Jul 16, 2014

If i have this code:

var test: Boolean;

test = false;

if (2 == 2 == 1)

{

    test = true;

}

trace (test);

Result: true.

var test: Boolean;

test = false;

if (2 == 2 == 2)

{

    test = true;

}

trace (test);

Result: false

var test: Boolean;

test = false;

if (2 == 2 == 3)

{

    test = true;

}

trace (test);

Result: false.

Why is that?

TOPICS
ActionScript
238
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 16, 2014 Jul 16, 2014

This is not a Math issue, it is a matter of how a condfitonal test is carried out.   Conditions are tested as pairs (with a left to right hierarchy unless otherwise arranged using parentheses), meaning for 2 == 2 == whatever, first the test is performed to see if 2 == 2 ?, which is true, meaning the result evaluates as a "true".  Then the next comparison is performed using the result of the first... which would be...  true == whatever ?. which depends on "whatever" is.  A 1 evaluates as true as

...
Translate
LEGEND ,
Jul 16, 2014 Jul 16, 2014

This is not a Math issue, it is a matter of how a condfitonal test is carried out.   Conditions are tested as pairs (with a left to right hierarchy unless otherwise arranged using parentheses), meaning for 2 == 2 == whatever, first the test is performed to see if 2 == 2 ?, which is true, meaning the result evaluates as a "true".  Then the next comparison is performed using the result of the first... which would be...  true == whatever ?. which depends on "whatever" is.  A 1 evaluates as true as far as logic goes, but any other number does not, so true == 1 = true, true == 0,2,3,4,5.... = false

You will find nothing passes the test if you reverse the order of the numbers.

If you want to do compound conditional tests you are best to do it in pairs and/or use parentheses to define the order in which evaluations are managed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 16, 2014 Jul 16, 2014

Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2014 Jul 16, 2014
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines