2

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!

1
  • This has got to be a dup of lots of questions... Commented Mar 13, 2011 at 20:35

8 Answers 8

3

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

1

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.

Comments

1

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

Comments

0

++ 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.

Comments

0

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;

Comments

0

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

3 Comments

say that it was ++x, what would my answer be then?
ok that makes sense. I see what I have to do for that. I have to do the 10 + 1 = 11 then times it by 5. y = 55 and x = 11 still or do they each need to have their own variables?
You can do several assigments in one line as suggested by all here, as ` z * x++ ` (one line) is the same as two lines of code: z * x; x = x + 1;, so you only have to write the style that better suits you.

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.