This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me to figure out confusion with my program output.

 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Guys.

Please help me to understand code given below. I tried to find the value for i. Few times i counted as 2. But finally i counted as 3 which is correct answer.
I just want to know, have i taken correct method to figure out the value for i.

This is how i counted.
b = false
i = 1
i++ increment i by 1. so new value of i is now 2.
b= !b means b's value assigned to true.
finally reach to while condition and its( value or b is) true. so loop continue.
increment i by 1. New value for i is 3
b= !b, Therefore b = false
Reach the while condition and its false. So go out from while loop and print 3.

Please help me to understand whether i have taken right path to figure out value of i.
Thank you in advance



 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think you have. I tried your code and got 3.
 
rayan alex
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to know whether i have followed correct path to get the output.
Thank you for you reply
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you reasoned correctly. Well done.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic