Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I have do while loop query ?  RSS feed

 
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 ?
 
author
Sheriff
Posts: 23484
138
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
 
Sheriff
Posts: 11818
193
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!