• 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

It should be an Infinite loop.......

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Options:
1)The code will fail to compile , while has an invalid condition expression
2)It will compile , but will throw Runtime Exception
3)It will print 3
4)It will go in an infinite loop
5)It will print 1
Correct Answer: 3)It will print 3
I feel that answer should be 4)It will go in an Infinite loop because b=!b is always going to be true and so i will be kept on incrementing,
Am I right?
Sonir
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried running this program?
Rob
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir,
b will not always be true.
b is initialized to false at first.
Then the body of the loop is evaluated and i is incremented (i's value is now 2).
Then the condition is evaluated:
b = !b means that the primitive variable b will be assigned the opposite value of the actual value held by b, that is b is false, !b is true so b gets assigned true. The loop goes for one more round, i is incremented (i's value is now 3) and the condition is evaluated. b gets assigned the opposite of its actual value, that is b gets assigned !true which is false and the condition evaluates to false, the loop finishes and 3 is printed...
HIH
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val,
I still cannot understand why wont B always remain true.Is there anything to indicate that the value of 'b' should be changed, is there any incrementation or etc...
I feel there is nothing to tell us that we have to change the value of 'b'.
b gets false first .Then i is incremented to 2 . and then it just checks whether b=b! i.e false is not true..(Which is true) ..Even after incrementing the value of 'i', it again checks the same stuff..
How can you say that the after incrementation, the value of 'b' will change?? i.e it will put the opposite value.
Val, please make me Understand..
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonir,
I think if you take a closer look at b=!b you will see what Val means. '=' is an assignment operator. In other words, read it like this: 'b gets the value not b'. What I think you see is 'b equals not b' which would be written in code like this: 'b == !b'. Note the extra equal sign!
Hope this helps,
--Kelley

Originally posted by sonir shah:
Val,
I still cannot understand why wont B always remain true.Is there anything to indicate that the value of 'b' should be changed, is there any incrementation or etc...
I feel there is nothing to tell us that we have to change the value of 'b'.
b gets false first .Then i is incremented to 2 . and then it just checks whether b=b! i.e false is not true..(Which is true) ..Even after incrementing the value of 'i', it again checks the same stuff..
How can you say that the after incrementation, the value of 'b' will change?? i.e it will put the opposite value.
Val, please make me Understand..

 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sonir,
Kelley is right, look more closely at the condition which is b=!b. We have here an assignment operator "=". That means that b will get the opposite of its current value. Then the whole expression is evaluated to the value of the newly assigned boolean variable b.
if b is false, then b=!b will assign true to b and the whole expression is evaluates to true.
Then b is true and b=!b will assign false to b and the whole expression is evaluates to false.
HIH
reply
    Bookmark Topic Watch Topic
  • New Topic