-1
class Test {
    public static void main(String[] args) {
        for (int i= 1; i < 2; i++) {
            System.out.println(i);
            System.out.println(++i);

            int a = 1;
            System.out.println(a++);
            System.out.println(++a);
        }
    }
}

The output here is 1,2 and 1,3. We are doing post-increment and pre-increment in both the cases why I am getting different results?

  • you need to know about post and pre incremental operator in java – Mr. Roshan Jul 26 '18 at 06:27
  • Use a debugger, and see how values are changing with iterations... – ernest_k Jul 26 '18 at 06:27
  • 1
    `for (int i= 1; i < 2; i++)` <-- because this `i++` gets executed after each iteration. Please have a look at the [docs](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). – BackSlash Jul 26 '18 at 06:30
  • Dupicate suggestion would have to be in context of loop, not only pre/post incrementation – Antoniossss Jul 26 '18 at 06:36
  • 2
    The loop here is totally irrelevant. – Andy Turner Jul 26 '18 at 06:40
  • @AndyTurner It isn't. The OP is asking why `a++ ... ++a` behaves different than `i++ ... ++i`, and the reason is that `i++` doesn't get executed until the end of each iteration, while `a++` gets executed immediately (so `i` and `a` will have different values). – BackSlash Jul 26 '18 at 06:47
  • @BackSlash Even if it were executed, the result would be 2,3 so the question doesn't make sense. – shmosel Jul 27 '18 at 04:22

5 Answers5

3

Because post increment happens at the end of the iteration.

Hence the below code :

for (int i= 1; i < 2; i++) {
        System.out.println(i);
        System.out.println(++i);

Will work something like this:

int i = 1;
System.out.println(i); // i is still 1
System.out.println(++i); // pre increment making i to 2 and then print
i++;
if(i<2) 
// iterate again

However, in second case:

int a = 1;
  System.out.println(a++); // post increment first print 1 and then change a to 2
  System.out.println(++a); // pre increment a now changed to 3
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
2
    System.out.println(i); // 1 (initial value)
    System.out.println(++i); // 2 (pre increment returns the value after increment)

    int a = 1;
    System.out.println(a++); // 1 (initial value prior to increment, since post increment 
                             // returns the value prior to increment)
    System.out.println(++a); // 3 (pre increment returns the value after increment)

The i++ in the loop's definition only takes place at the end of each iteration. You never print the value of i after i++ is executed, since the loop terminates after the first iteration.

BTW, if i++ was executed prior to System.out.println(i);, you'd still get a different output (compared to the last two println statements) - 2 followed by 3.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

The general structure of a for loop (more precisely: this type of basic for statement) is:

for ([ForInit]; [Expression]; [ForUpdate]) Statement

The equivalent while loop is:

[ForInit];
while ([Expression]) {
  Statement;
  [ForUpdate];
}

In other words, the update is executed after the loop body.

So your code is equivalent to:

// ForInit
int i = 1;
while (/* Expression */ i < 2) {
  // Statement
  System.out.println(i);
  System.out.println(++i);

  int a = 1;
  System.out.println(a++);
  System.out.println(++a);

  // ForUpdate
  i++;
}

Hopefully it is then easy to see why there is a difference in the output.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

In the first case, the post-increment is not called. If you understand how a for loop works, you will know that the increment section is not executed the very first time. The initialization section, here int i = 1, and the condition checking - i < 2 is validated. The increment section is invoked only after each iteration.

In the second case, the post-increment is performed and hence the difference in values. You should focus more on the basics of looping.

kishore
  • 604
  • 3
  • 7
  • 13
0

System.out.println(i);

i isn't postincremented here. The answers would be the same if it were:

System.out.println(i++);
System.out.println(++i);

int a = 1;
System.out.println(a++);
System.out.println(++a);



for (int i= 1; i < 2; **i++)**

doesn't do anything until a pass through loop is done.

hamenaglar
  • 63
  • 6