• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

increment prefix in loop

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am abit confused about the increment prefix when used in loop condition:



i was expecting output 1,2,3,4,5,6,7,8,9

why do i get 0,1,2,3,4,5,6,7,8,9 i was expecting i to be incremented b4 being used in the sys.out statement

yet in an if it behaves how i would expect eg


the condition evaluates to true , ie its incremented first
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a for loop, the "increment" statement is executed at the "bottom" of the loop -- that is, at the end of the loop body.

It doesn't matter whether you use a prefix or a postfix, because in both cases, the variable will hold the incremented value by the time the boolean test is performed at the top of the next iteration.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basically, a for is like this:

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

to avoid these steps, we write it in a single line like:
for(int i=0;i<10;i++){
System.out.println("i= "+i);
}

I think you got the point.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As marc and krishna said, the loop variable updation (increment/decrement) is done at the last step in case of for loop.

But its not the case in if loop.

Just change the prefix to postfix in your if loop, it will not work.



You will get the else part as an output in this case, because the comparison is done first and then the increment of the variable "i" happens. The same case with for loop - always!

HtH.
[ May 23, 2007: Message edited by: Raghavan Muthu ]
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me first try to understand the for loop .here comes the explanation

for (initialization; termination; increment) {
statement(s)
}

When using this version of the for statement, keep in mind that:
(Note)1:The initialization expression initializes the loop; it's executed once, as the loop begins.
(Note)2:The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

As mentioned the "loop gets excecuted once as loop begins" whatever the first value you enter will be entered first in your program than second time the increment expression gets excecuted.Than whatever is the increment postfix or prefix it does not matter in for loop.
i hope this explanation helps you to understand the concept.
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<snip>
int i=0;
if(i++==1){
System.out.println(" i is equal to 1");
}
else
System.out.println(" i is not equal to 1");
You will get the else part as an output in this case, because the comparison is done first and then the increment of the variable "i" happens.
</snip>

I think here, first increment will takes palce and then comparison will be done. hence, the result is else part.

If in this case:
if(++i==1)
First, comparison will be done and then increment will takes place.

basically, i++ means i=i+1.
so, if(i++==1) will resolve to if((i=i+1)==1)
 
Louis Moloney
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the great responses guys , i got it now
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,

Your statement does not convey a CORRECT MESSAGE!


-- Stmt 1 --
I think here, first increment will takes palce and then comparison will be done. hence, the result is else part.

-- Stmt 2 --
If in this case:
if(++i==1)
First, comparison will be done and then increment will takes place.

-- Stmt 3 --
basically, i++ means i=i+1.
so, if(i++==1) will resolve to if((i=i+1)==1)



Let me come 1 by 1..

Stmt 1:

If at all the increment had taken place, how come the else part would be printed? It would be the "if" part right? There itself it contradicts.

Stmt 2:
Same as Stmt 1. "++i" means prefix - means the increment operation is done first and then anything else with that operand!

Stmt 3:
if(i++==1) will resolve to if((i=i+1)==1) -> Your statement itself shows that increment is done first prior to comparison.

Look at the following piece of code.



Based on the explanations provided above, the output of running the piece of code is as follows.



Hope this clears.
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by krishna:
<snip>
int i=0;
if(i++==1){
System.out.println(" i is equal to 1");
}
else
System.out.println(" i is not equal to 1");
You will get the else part as an output in this case, because the comparison is done first and then the increment of the variable "i" happens.
</snip>

I think here, first increment will takes palce and then comparison will be done. hence, the result is else part.

If in this case:
if(++i==1)
First, comparison will be done and then increment will takes place.

basically, i++ means i=i+1.
so, if(i++==1) will resolve to if((i=i+1)==1)



It is just the opposite.

- ++i --> Increment i and then evaluate
- i++ --> Evaluate i and then increment

if(i++==1) will resolve to if(i==1) doSomething(); i++;
if(++i==1) will resolve to if((i=i+1)==1) doSomething();
 
krishna bulusu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry......
I have confused!!! yes i am wrong. My Project Manager has come at that time !!

Thank you for correcting me!!!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats okay Krishna.

Project Managers are meant to deviate the Programmers is it?
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
the third part in the "for loop()" was execute after the "for loop body{}.
in other words:"++i" was execute afrer "{ System.out.println(" i = "+i);}" every time.
hopes it will be usefull for you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic