Dan
Dan

Reputation: 21

JavaScript code unexpected results after incrementing numbers

I am trying to solve this out:

x = 10; y = 0; z = 5;

y = z * x++

y = z * ++x

The instruction maunal (500 pages long) that I am reading all it tells me is that the answer to the x++ problem is:

y = 50 and x = 11. 

I know how to get the x++ all you have to do is add an increment of 1.

x = x + 1.

Can someone please help me, because I cant figure out what I am doing wrong! I am not understanding where the book got 50 from!

Upvotes: 2

Views: 73

Answers (8)

edgarmtze
edgarmtze

Reputation: 25058

First you have

x = 10;
y = 0;
z = 5;

then you do:

y = z * x++  = 5 * 10 = 50

after that you get:

y = 50;
x = 11; (because x = x+1)

then:

y = z * ++x  = 5  * 11 = 55

Upvotes: 0

Artur Iwan
Artur Iwan

Reputation: 1210

Example:

a * (b++)

It's the same as:

a * b;
b = b + 1; //POST increment

but

a * (++b);

is the same like this:

b = b + 1; //PRE increment
a * b;

Upvotes: 0

Hamish
Hamish

Reputation: 23346

++ after the variable is a post-increment operation, that is, x is incremented after the assignment is made.

In other words, y is calculated to be 50 and then x is incremented.

Upvotes: 0

Andrew Moore
Andrew Moore

Reputation: 95434

The problem you are having here deal with post-incrementation and pre-incrementation.

When doing x++, you are post-incrementing... This means that the incrementation will only occur after the statement has been evaluated.

So, given the following code:

x = 10; y = 0; z = 5;

y = z * x++;

JavaScript does this:

x = 10; y = 0; z = 5;

y = z * x++;

// Ignore Post-Increment, Evalutate
y = z * x;
y = 5 * 10;
y = 50;

// Now Increment x - POST-INCREMENT
x = x + 1;
x = 10 + 1;
x = 11;

When doing ++x, you are pre-incrementing... This means that the incrementation will occur before the statement is evaluated:

x = 10; y = 0; z = 5;

y = z * ++x;

// Do Pre-Increment
x = x + 1;
x = 10 + 1;
x = 11;

// Evaluate
y = z * x;
y = 5 * 11;
y = 55;

Upvotes: 3

Mark Byers
Mark Byers

Reputation: 838974

The result of a post-increment expression is the value before the variable has been incremented.

So this:

y = z * x++;

is roughly equivalent to:

y = z * x;     // y = 50
x = x + 1;     // x = 11 (the increment is done afterwards)

On the other hand, a pre-increment operation evaluates to the value after the increment. So this:

y = z * ++x;

is roughly equivalent to this:

x = x + 1;     // x = 11  (the increment is done first)
y = z * x;     // y = 55

Upvotes: 2

DhruvPathak
DhruvPathak

Reputation: 43265

It is post incrememt operator. x++ use value of x, then increment it ++x ( pre increment) increment x, then use it

Upvotes: 1

adarshr
adarshr

Reputation: 62603

You should understand the difference between post-increment and pre-increment operators here.

In the y = z * x++ case, x if first used in the expression and later incremented. Thus the value of the expression becomes y = 5 * 10 after which the value of x is incremented to 11.

In the other case, the pre-increment operator is used. So it's first incremented and used later. Thus the value becomes 55 in that case.

Upvotes: 1

bensiu
bensiu

Reputation: 25604

x++ increase x by after value is delivered:

 y = z * x++   // 5 * 10  // and 10 become 11 after operation

++x increase x by before value is delivered:

 y = z * ++x   // 5 * 11  

fancy names post and pre incrementations

Upvotes: 1

Related Questions