user1419012
user1419012

Reputation: 21

Basic Incrementation

i know this is a very basic question BUT.

I understand the concept behind. n++, ++n, n--, --n. HOWEVER

public static void main(String[] args){

    int count = 1;
    for(int i =1;i<=10;++i){

    count = count * i;
    System.out.print(count);
    }
}

So it will print: 1 2 3 4 5 6 7 8 9 10.

My Question is. Why if i is incremented as ++i isnt i then treated as 2, instead of 1. Inst the point of ++i, to increment i before it's manipulated by another operation?

Upvotes: 1

Views: 254

Answers (7)

Jon Skeet
Jon Skeet

Reputation: 1499760

Is the point of ++i, to increment i before it's manipulated by another operation?

The difference between ++i and i++ only matters when it's used as part of a bigger expression, e.g.

int j = ++i; // Increment then use the new value for the assignment
int k = i++; // Increment, but use the old value for the assignment

In this case the operation occurs at the end of each iteration of the loop, on its own. So your loop is equivalent to:

int count = 1;
// Introduce a new scope for i, just like the for loop does
{
    // Declaration and initialization
    int i = 1;
    // Condition
    while (i <= 10) {
        count = count * i;
        System.out.print(count);

        // Now comes the final expression in the for loop "header"
        ++i;
    }
}

Now changing ++i to i++ at the end there isn't going to make a difference at all - the value of the expression isn't used for anything.

Upvotes: 11

Brian
Brian

Reputation: 25823

As an addition to the other answers, the historical reason for preferring for(int i=1;i<=10;++i) over for(int i=1;i<=10;i++) is that ++i does not need to store the old value of i in an extra variable. Thus, ++i is faster than i++, though the speed improvement is negligible. On modern compilers this speed improvement is done as an optimization, so the two pieces yield the same compiler output. However, since ++i is always as fast or faster (e.g., on old C++ compilers) than i++, many experienced programs always use ++i within loops.

As other answers have stated, both pieces of code are functionally equivalent (in the case of a for loop).

Upvotes: 0

ShWebb
ShWebb

Reputation: 39

For i = 0 and While i < 1= 10, print i, and then pre-increment i. (++i/i++ doesn't make a difference here).

Here try this though:

int i=1;  
while(i <= 10)  
  System.out.print(++i);  


i = 1;  
while (i <= 10)  
  System.out.print(i++);

Upvotes: 0

Angel
Angel

Reputation: 1200

The for loop you wrote is same as:

i = 1;
while(i<=10) {
  count = count * i;
  System.out.print(count);
  i = i + 1;
}

So that's why!

Upvotes: 0

Chris
Chris

Reputation: 8170

In this case ++i happens at the end of the loop, it increments and then checks if the new value still meets the termination condition.

Also, won't the output be:

count   i  
1   *   1 = 1
1   *   2 = 2  
2   *   3 = 6   
6   *   4 = 24  

etc.

Upvotes: 0

user1432824
user1432824

Reputation: 209

You want to use i++ which is a post increment. ++i is called a preincrement and the difference is precisely as you have pointed out.

Upvotes: 0

SimplyPanda
SimplyPanda

Reputation: 725

The increment isn't called until after the first iteration of the for loop.

While it's true that

j = i++;
k = ++i;

return different results, think of the ++i in this context as a standalone line called at the end of every for loop.

Upvotes: 3

Related Questions