-3

I try this below program which find midpoint between two no.

class FindMidPoint

{

public static void main(String args[])

{

int i, j;

i = 100;

j = 200;


// find midpoint between i and j

while(++i < --j) ;

System.out.println("Midpoint is " + i+" OR "+j);

}

}

Output:

Midpoint is 150 OR 150

But If I make changing in while condition that is

while(i++<j--)

Then output is:

Midpoint is 16 OR 14

I also refer the below Link then also I'm not understanding the difference between ++i & i++ in this program.

what is the difference between i++ & ++i in for loop (Java)?

Please explain me if anyone have idea about.

Community
  • 1
  • 1

5 Answers5

0

If you have i = 1 and j = 2. The first one compares 2 < 1 and the second one compares 1 < 2. (In the first one the decrementation/incrementation are done before the comparing, while in the second one they are done after the comparison)

Slow Trout
  • 492
  • 3
  • 13
0

The ++ before any variable present in an expression indicates that increment has to be made before the expression executes.

For example

int i=2;
int x = ++i + 2

Here since ++ is present before i, so first there will be an increment on i, and then the expression will be executed. The steps involved is :

Step 1 :

++i : i=(i+1) = 3

Step2:

int x = ++i + 2 = 3+2=5

So after this expression executes : x=5 and i=3

But if you consider post increment:

For example:

 int i=2;
 int x = i++ + 2;

This means that increment will happen after the line is executed. The steps are:

Step 1 :

int x = i++ + 2 = 2 + 2 =4; // i is still 2 here since the increment didn't happen by now

Step 2:

i++ : i=i+1 = 3;

So after the execution x=4 and i=3

Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
0
++i

You could think something like increment the value and then use the incremented value in the expression

i++

Use the value in the expression before it gets incremented

Say we have the following code:

int i = 100;
int j = ++i + 50;  // statement 1

Here j would have a value of 151, saying increment i first and then evaluate the expression, so 101+50.

Lets way we have

int j = i++ + 50;  // statement 2

Here j would have a value of 150, saying evaluate the expression first, then increment the value i

However, i would be 101 after executing the second statement

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
HJK
  • 1,382
  • 2
  • 9
  • 19
0

Its all about precedence, in java every operator has a precedence, pre-increment has very high precedence while post-increment come at last as compared to other operators.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
0

Basic difference between pre and post should be applied here.

Pre increment operator ++i : equivalent to replacing i with i+1 and using result in the expression.

while(++i < --j);
//read as while( (i_new = i_old+1) < (j_new =j_old -1) );
//In next expression i will be value of i_new and j value will be j_new

Post increment operator i++ : equivalent to using the same value of i in the current expression. And in the next immediate expression use the increased value.

while(i++ < j--);
//read as while( (i_old < j_old );
//In next expression(even in next iteration of same above while) i will be value of i_old+1 and j value will be j_old-1
sbm
  • 81
  • 6