as3 increment and decrement operator


What will be the result of snippet in AS3 ,Guess?

var a:int = 1;
a = ++a + a++ + a;
a = a– + –a;
a = ?

Solution

var a:int = 1;
// a == 1
a = ++a + a++ + a;
// ++a is pre increment operator(first increment then assignment) and a++ is post increment operator(first assignment then increment) as3 compiler parse left to right so first ++a then a++ and finally a so result become
// 2 + 2 + 3 = 7

a = a– + –a;
// a– is post decrement operator and –a is pre decrement operator
// 7 + 5 = 12

a = ? // 12

there is a Gotcha ,if you use c concept then you will give wrong answer, because as3 parse From left to right (opposite to c language)
if have any confusion please comment i will try to answer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s