Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Control flow

 
Netty poestel
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here's how I'm seeing this.
code starts, i is incremented in the 1st iteration and = 2
then comes 'while(false is not equal to false)'
....I am lost hereafter.
btw answer = 3.
any pointers ?

TIA
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, a do loop always permits the first iteration, so i becomes 2.

"while (b = !b)" is tricky. It contains an assignment expression, not a conditional expression.

If an assignment operator (=) is used in an expression, the value of the right hand side of = becomes the result of the operation.

In this case, "b = !b" takes b, which has the value false, negates it, giving the value true, and stores true in b. However, the value true is also used as the value of the expression "a = !b", so "while(b = !b)" is true and the second iteration of the do loop is permitted. Now i becomes 3.

Now, b is true, so b becomes false and while("b = !b)" is false. So the do loop falls through and i remains 3 and is printed as 3.
[ November 22, 2004: Message edited by: Mike Gershman ]
 
Netty poestel
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
El Perfecto !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic