0

In my computer science exam there was a question as follows:

i=10, j=51 & k=7.
p = i+(--k +j) + (3*(i++))+i.

I am getting the answer as 111. Here's my methodology :

k is decreased by one in (--k +j), and i in (i++) is increased, so (3*(i++))+i becomes (3*11)+11. But when i entered the statements into BlueJ, the answer came as 108. Why can't i be incremented in the brackets? According to my textbook the parentheses is higher in importance than ++. Is my textbook wrong? Or am i going wrong somewhere?

closed as off-topic by gnat, Neil, jwenting, amon, esoterik Jul 25 '18 at 19:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in explaining, writing or debugging code are off-topic here. These can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question (see Minimal, Complete, and Verifiable Example)." – gnat, Neil, jwenting, amon, esoterik
If this question can be reworded to fit the rules in the help center, please edit the question.

  • The problem with this expression (regarding maintainability, not necessarily Java language rules) is not that i++ used inside a larger expression, but that both i and i++ are used in the same expression -- no sane programmer would write that! – Erik Eidt Jul 24 '18 at 16:32
5

You're confusing two different rule sets that deal with ordering things.

Operator precedence determines which operator in a complex expression is evaluated before the other. Multiplication takes precedence over addition, which is why 1 + 2 * 3 evaluates to 7, not to 9. Parentheses take even greater precedence, so that (1 + 2) * 3 does evaluate to 9.

Sequencing determines which operation in a program occurs first; this is not the same thing. For instance,

print('a'); print('b');

guarantees that its output is "ab" and not "ba", because the semicolon introduces a sequence point, across which operations may not be reordered no matter how smartly the compiler tries to optimize your program.

The semantics of pre- and post-increment ++ fall into this rule set. As a rule, i++ guarantees that the change to the value of i happens only after the old value has been used in computing the value of the expression where it occurs. Therefore, the expression always returns a value based on the old, lower value of i.

Because all of this is rather complicated, but indispensasble to create a reliable programming language, the conventional wisdom is not to use ++ and related operators with side effects within complex expressions. Even if you understand how all the rules interact, the next maintenance programmer may not, and maintainability is the second most important property of software (right behind correctness).

  • And according to some schools of thought, simplicity is more important than correctness. – Neil Jul 24 '18 at 7:51
  • to resume we can imagine the ++ operator as a function while having side effect, the operator return a value which might not be the one that you think. For our post increment operator, you would write the function like this : int f(int *i){int a=*i;*i=*i+1;return a; } – Walfrat Jul 24 '18 at 8:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.