Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

post/pre increment operator

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
  • 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: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
I don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic