• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

post/pre increment operator

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the post and pre increment operator work when used on a for loop? The output of the following example appears to be the same



I was expecting the second loop to start from 1.

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:How does the post and pre increment operator work when used on a for loop? The output of the following example appears to be the same



I was expecting the second loop to start from 1.




Increment operators have a value and an effect. In both cases, the effect is the same -- the variable will be incremented. The value however, are different. With the post increment, the increment happens after, so the value is before the increment. With the pre increment, the value is after the increment.

In your example, you are not using the value -- if is simply thrown away. Hence, there is no difference in your output.

Henry
 
Ranch Hand
Posts: 72
Android Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The iteration expression is executed after each execution of body
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhas Mandrawadkar wrote:The iteration expression is executed after each execution of body



Look at this example and see the output difference now...


Output:
0123456789
12345678910
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are printing is not iteration expression. You are printing y. Now ++y in a SOP statement will increment it before printing.
Your iteration expression is ++x, and it is still evaluating the body ten number of times

for(initialization and declaration of loop variable; condition expression; iteration expression)
{
//Printing variable x
}

 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhas Mandrawadkar wrote:What you are printing is not iteration expression. You are printing y. Now ++y in a SOP statement will increment it before printing.
Your iteration expression is ++x, and it is still evaluating the body ten number of times

for(initialization and declaration of loop variable; condition expression; iteration expression)
{
//Printing variable x
}



I was agreeing with you lol..Was showing that in order to get what he wanted he'd need to have the pre increment within the body of the for loop since it executes first.
 
please buy this thing and then I get a fat cut of the action:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic