1

shouldn't this be, 11 is good and 21 is bad? because, in case of i++, the value 10 first gets evaluated if equal to i, and then incremented?

int i = 10;
if(i++ == i){
 System.out.println(i + " is good");
}
else{
 System.out.println(i + " is bad");
}

int j = 20;
if(++j == j){
 System.out.println(j + " is good");
}
else{
 System.out.println(j + " is bad");
}

Output: 11 is bad 21 is good

Queenish01
  • 51
  • 4

3 Answers3

4

In i++ == i the left hand side evaluates to 10, increments i, and the right hand side evaluates to 11, because i has been incremented. So the equality is false.

In ++j == j the left hand side increments j and evaluates to 21, and the right hand side evaluates to 21, because j has been incremented. So the equality is true.

khelwood
  • 55,782
  • 14
  • 81
  • 108
4

@khelwood's answer is correct, as far as it goes, but doesn't really tell you exactly what the pre and post increments 'are', as requested.

In the languages that have this, including Java, there are "expressions". x + y is an expression. So are ++i and i++.

The expression ++i evaluates to one more than the value i has when the expression is evaluated. As a side effect, the value of i is also set to that value when the expression is evaluated.

The expression i++ evaluates to the value that i has when the expression is evaluated. As a side effect, the value of i is incremented AFTER the expression is evaluated.

There is a history to this that I find interesting; it explains to some extent why the operator exists, though I don't think it really helps understand it, so unless you're interested in the history you can just skip it.

The C language was invented and first implemented by a few engineers at Bell Labs on computers built by Digital Equipment Corporation, aka DEC. These were some of the first smaller computers, well before the Personal Computer came along. In the machine language of some of their models, they had instructions which accessed memory by adding together some internal registers. They built the machine code so that one of the registers could be incremented after the memory access, so that it was then pointed to the NEXT memory location.

In the world of small computers at that time, both execution speed and code size were precious, so being able to write a tight loop to go through memory in the fewest instructions and at machine code speed was desireable. So DEC's computers -- including their very popular PDP-11 line -- had post-and-pre increment and decrement as 'addressing modes' with which machine code could access memory.

The guys implementing C, which was and has remained a structured assembler, not a high-level language, wanted to leverage this machine-level advantage from C itself. So that's why the language has pre and post increment and decrement, now having to be implemented in every compiler that supports the language.

arcy
  • 12,845
  • 12
  • 58
  • 103
2

Explanation of the example :

In post-incrementing the value is incremented after its evaluation .When you write i++ during the evaluation it first using the previous value of i i.e. 10 but after i++ is evaluated i is now equal to 11. So during the statement if (i++==i) it simply means if (10==11) and that's gonna return false.

In pre-incrementing the value is incremented before its evaluation. When you write ++i first i is gonna increment to 21 then it's used. It is same as writing if(21==21) and that's gonna return true.

New example for better understanding :

int i=10;
System.out.printf("Value of i during post-incrementing %d \n",i++);
System.out.printf("Value of i after post-incrementing %d \n",i);
System.out.printf("Value of i during pre-incrementing %d \n",++i);
System.out.printf("Value of i after pre-incrementing %d \n",i);

Output:

Value of i during post-incrementing 10
Value of i after post-incrementing 11
Value of i during pre-incrementing 12
Value of i after pre-incrementing 12