• Post Reply Bookmark Topic Watch Topic
  • New Topic

I have do while loop query ?  RSS feed

 
Vijairam Ganeshmoorthy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the code ....


I get the outcome as 2

I get the outcome as 1 .

How is this possible ?  what's the difference between the two ?
 
Henry Wong
author
Sheriff
Posts: 23291
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijairam Ganeshmoorthy wrote:
How is this possible ?  what's the difference between the two ?


This question has nothing to do with the loop. The difference is in the post increment operator... and how the increment is applied after the variable (increment) has been evaluated.

... or in other words... while the variable is incremented, the value of the expression "i++" is the value of the variable before the increment.

Henry
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Henry's point, that code will behave exactly the same way without the do while loop around it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!