James Chegwidden

Author

Ranch Hand

Ranch Hand

Posts: 201

posted 13 years ago

I was working on notes on prefix/postfix evaluation and I came across Maha Anna way of evaluation of prefix/postfix from previous posts.

I can see how she does it, but the way its word does not seem to be correct.

prefix/postix

I am looking to clean up the wording and show my students this method (with appropriate credit)

Examples,

Given a= 10;

1. x= a++ + ++a; //x= 22

Given a = 2, b = 3, c =2

2. x= ++a - b++ + ++c //x = 3

To me, This is basically what she said:

1. for any i, subsitute the latest value of i

2. for ++symbol, increment the latest value

3. for -- symbol, decrement the latest value

4. finally do the calculation for all non brackted values

5. be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement. In this case the assigned var takes its lastest value. (Which was corrected to say: Since the left hand side var happened to be the var i inteself it takes the value of calculated value.

So

1. 10(11) + (12)12 = 22

2. x= (3)3 - 3(4) + (3)3 = 3

Any help on cleaning up the English with this is helpful (Blindly is a bad word to use!!)

[ July 03, 2004: Message edited by: James Chegwidden ]

I can see how she does it, but the way its word does not seem to be correct.

prefix/postix

I am looking to clean up the wording and show my students this method (with appropriate credit)

Examples,

Given a= 10;

1. x= a++ + ++a; //x= 22

Given a = 2, b = 3, c =2

2. x= ++a - b++ + ++c //x = 3

To me, This is basically what she said:

1. for any i, subsitute the latest value of i

2. for ++symbol, increment the latest value

3. for -- symbol, decrement the latest value

4. finally do the calculation for all non brackted values

5. be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement. In this case the assigned var takes its lastest value. (Which was corrected to say: Since the left hand side var happened to be the var i inteself it takes the value of calculated value.

So

1. 10(11) + (12)12 = 22

2. x= (3)3 - 3(4) + (3)3 = 3

Any help on cleaning up the English with this is helpful (Blindly is a bad word to use!!)

[ July 03, 2004: Message edited by: James Chegwidden ]

Author and Instructor, my book

Marilyn de Queiroz

Sheriff

Posts: 9080

12

posted 13 years ago

I also found this very helpful when I was studying for the SCJP.

Here is a simple (easy) method to solve this type of situation. Just follow these rules, and you will get the correct answer.

Wherever you find the variable, just substitute the latest value. When you find ++/-- operator, irrespective of post/pre increment/decrement (++/--), substitute the increment/decrement of the latest value of that variable in parentheses.

Then do the calculation with the substituted values, leaving out the values in the parens. You have to start from left to right.

So let's start

Easy, isn't it

regards,

maha anna

[ July 04, 2004: Message edited by: Marilyn de Queiroz ]

**Originally posted by maha anna:**Here is a simple (easy) method to solve this type of situation. Just follow these rules, and you will get the correct answer.

Wherever you find the variable, just substitute the latest value. When you find ++/-- operator, irrespective of post/pre increment/decrement (++/--), substitute the increment/decrement of the latest value of that variable in parentheses.

Then do the calculation with the substituted values, leaving out the values in the parens. You have to start from left to right.

So let's start

Easy, isn't it

regards,

maha anna

[ July 04, 2004: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt