• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Control flow

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
El Perfecto !
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic