?
Solved

Pre-increment Question

Posted on 2003-03-22
13
Medium Priority
?
645 Views
Last Modified: 2009-07-29
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
Comment
Question by:ZeeshanSyedJC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8188791
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
 
LVL 92

Expert Comment

by:objects
ID: 8188795
> 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
 

Author Comment

by:ZeeshanSyedJC
ID: 8188807
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 92

Expert Comment

by:objects
ID: 8188832
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
 
LVL 92

Expert Comment

by:objects
ID: 8188915
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
 
LVL 1

Expert Comment

by:EnPassant
ID: 8189465
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
 

Author Comment

by:ZeeshanSyedJC
ID: 8190275
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
 
LVL 1

Expert Comment

by:EnPassant
ID: 8190620
++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
 

Author Comment

by:ZeeshanSyedJC
ID: 8191479
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
 

Author Comment

by:ZeeshanSyedJC
ID: 8191503
EnPassant :  By the way, the code you sited in you last note yields the same output in each loop execution.
0
 
LVL 1

Accepted Solution

by:
iseletsk earned 500 total points
ID: 8191617
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
 
LVL 92

Expert Comment

by:objects
ID: 8191896
> 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
 

Author Comment

by:ZeeshanSyedJC
ID: 8192035
That is just the sort of explanation I needed.  I understand now.  Thanks so much!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question