• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

Pre-increment Question

My question is why the pre-increment operator does not work properly when used as a parameter in a for loop, but works properly when used within the loop itself??

Consider the following:

          for (int i=0;i<10;)
             System.out.println(++i);

          System.out.println();

          for (int i=0;i<10;)
             System.out.println(i++);

      //-------------------------------------
          System.out.println();

          for (int i=0;i<10;i++)
             System.out.println(i);

          System.out.println();

          for (int i=0;i<10;++i)
             System.out.println(i);



0
ZeeshanSyedJC
Asked:
ZeeshanSyedJC
  • 5
  • 5
  • 2
  • +1
1 Solution
 
objectsCommented:
What do you mean it doesn't work properly.
The first loop should print:
1 2 3 4 5 6 7 8 9 10
The rest should print:
0 1 2 3 4 5 6 7 8 9
0
 
objectsCommented:
> for (int i=0;i<10;)
>    System.out.println(++i);

First loop:

i=0
++i -> 1 (i=i)

Next loop:

i=1
++i -> 2 (i=2)

...

until i=10

         
0
 
ZeeshanSyedJCAuthor Commented:
I am only referring to the first and fourth loops in the source code provided.  The following yield 2 different outputs:

         for (int i=0;i<10;)
            System.out.println(++i);

         System.out.println();


         for (int i=0;i<10;++i)
            System.out.println(i);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
yes I would expect them to behave differently.
The 1st example behaves as commented above.

The second behaves as follows:

First loop:

i=0
i -> 0 (i=0)

Next loop:

i=1
i -> 1 (i=1)

...


0
 
objectsCommented:
The primary difference is that if ++i is in the for loop then it does not get executed until the end of the loop.
0
 
EnPassantCommented:
If you say

x = 1;

y = 1;

now say x = y++;

x is 1;
y is 2;

but if you say x = ++y;

x is 2;

y is 2;

Postincrement executes the assignment, THEN increments y.

Preincrement increments y FIRST and then executes the  assignment.


~mgb
0
 
ZeeshanSyedJCAuthor Commented:
Object:   Do you mean that if the ++i is a prt of the for loop parameter list, then it won't be executed until the end of the loop, but if it is within the body, then it will
decrement first and then be evaluated?
0
 
EnPassantCommented:
++i will increment i and then execute subsequent commands with an incremented value of i.

i++ will execute the subsequent commands (those in the body of the loop) and then increment i.

Do a print out of

for(i = 0; i < 10; i++)
print(i);

then do a print of

for(i = 0; i < 10; ++i)
print(i);

and you will see the difference.

~mgb
0
 
ZeeshanSyedJCAuthor Commented:
EnPassant :  I am aware of how i++ behaves.  My confusion is when I use ++i as a for loop parameter, it behaves exactly the same as i++.  Conversely, when I put it within the set of statements WITHIN the for loop, it behaves as expected.
0
 
ZeeshanSyedJCAuthor Commented:
EnPassant :  By the way, the code you sited in you last note yields the same output in each loop execution.
0
 
iseletskCommented:
any for loop like
for (decl; cond; inc) {
....
}
can be translated to:
decl;
while (condtion) {
....
inc;
}

Look at it this way:

 for (int i=0;i<10;)
            System.out.println(++i);

Can be translated to:
int i = 0;
while (i<10) {
  ++i;
  System.out.println(i);
}

 for (int i=0;i<10;)
            System.out.println(i++);
Can be translated to:
int i = 0;
while (i<10) {
  System.out.println(i);
  i++;
}

for (int i=0;i<10;i++)
            System.out.println(i);

->
int i = 0;
while (i<10) {
  System.out.println(i);
  i++;
}

      for (int i=0;i<10;++i)
            System.out.println(i);

->
int i = 0;
while (i < 10) {
  System.out.println(i);
  ++i;
}

Note that in the last two cases, increment (post or pre) is done after System.out.println is called.
0
 
objectsCommented:
> Do you mean that if the ++i is a prt of the for loop
> parameter list, then it won't be executed until the end
> of the loop, but if it is within the body, then it will
> decrement first and then be evaluated?

Yes the ++i will not be executed until the end of the loop.
This is how a for statement works, the last clause of the for loop is run at the end of the loop, not at the start.
If it is in the body it is executed when that line is reached. There is no difference in *how* ++i is evaluated.

int x = 0;
for (int i=0; i<=0; x=6)
{
   System.out.println(x);  // x = 0
   i++;
}
System.out.println(x);  // x = 6
0
 
ZeeshanSyedJCAuthor Commented:
That is just the sort of explanation I needed.  I understand now.  Thanks so much!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now