The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Control flow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Control flow" Watch "Control flow" New topic

Control flow

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131

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 ?

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
El Perfecto !
I agree. Here's the link:
subject: Control flow
jQuery in Action, 3rd edition