Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Increment & Decrement Operator in Expressions

  Asked By: Caleb    Date: Sep 08    Category: Java    Views: 622
  

I'm rereading the beginning chapters of the textbook by Walter
Savitch and in Chapter 2, there is a paragraph about increment and
decrement operators in expressions.

I understand, on page 67 and 68, what the paragraph said about the
following and how the result is 15:

int n=3;
int m=4;
int result;
result=n* ++m;
result = 3 * (m=4+1)
result = 3*5
result =15

I understand that the ++m increases the value of m by 1 before it
does the multiplication.

What I don't understand is why is the answer to the next expression
12 instead of 13?

int n=3;
int m=4;
int result;
result=n* m++;
result = 3*(m=4+1)
result = 3*4
result =12

In this second expression, the book says that if the ++ is after the
m, then the value of m is increased after its value is used in the
expression. What happened to the increase of 1 in "4+1". Do you
just discontinued the rest of the value in m=4+1. My way of
thinking would be:

result=3*(m=4+1)
result=3*(4)+1

I just can't understand why m=4+1 stays a 4 all the time. What
happens to the 1?

I appreciate any instruction. I'm trying to get through chapters 1
through 5 before I take my next semester of Java.

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Increment & Decrement Operator in Expressions Or get search suggestion and latest updates.