• 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

Flow Control and Evaluating boolean

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the foll code:
class Trial
{ public static void main(String args[ ] )
{ boolean b = false ; int i = 1 ;
do
{ i++ ; } while (b=!b) ;
System.out.println(b = !b); System.out.println( i );
}
}
the result is i = 3. HOW ???
it goes through the do loop , i becomes 2, then it checks for
while : b = !b should evaluate to b = true --- and then it should enter a perpetual loop. but this doesn't happen - WHY?? then what is the boolean condition in the while loop?? if it is false then i should be = 2.
how does it become 3 ???

------------------
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First run :
i = 2
while (b=!b) Here b is assigned !false ie true
So it will loop again
Second run
i = 3
while (b=!b) Now b is assigned !true ie false
Therefore while exits
So you get i = 3
And System.out.println will print true
 
Shailendra Guggali
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx pravin -
how could i miss that boolean b will also change in the
subsequent loop
shail
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pravin Panicker:
First run :
i = 2
while (b=!b) Here b is assigned !false ie true
So it will loop again
Second run
i = 3
while (b=!b) Now b is assigned !true ie false
Therefore while exits
So you get i = 3
And System.out.println will print true


I am missing something here, will the while loop evaluate value of b or of expression b=!b.
I am confused, I know the answer mentioned above is correct but just wanna know how while works in java ?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurav,
What you might be missing is the the while(b=!b) only contains one = sign its a common mistake that I am just braking my self which is to mistake things like (b=!b) as (b==!b) if you solve for (b=!b) it and b=true it workd out like this
b= (!true)
b=false there fore it would be the same as while(false) which will always exit the while.
Hope that helps!
Lee
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lee.
I see your point of = and ==, let me try to explain what I am trying to point out
case 1. while( (b=!a) )
case 2. while( (b==!a))
In case 1 the result of inner expression (b=!a), should be true if b can be assigned value of !a.
In case 2 the result of inner expression (b==!a), should be true if b is equal to !a.
So what I am trying to say is how does while(b=!a) treated in java?
Does it assign the value to b and check whether b is true or false?
OR
It checks whether the expression (b=!a) was true or not, I mean if there was a condition say (b>a) where b=5 and a is 3 will it return true?
I am a C/C++ background, and I think in C/C++ statement
like while(a=5) will put it in infinite loop.
 
Lee Clarke
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


case 1. while( (b=!a) )
case 2. while( (b==!a))
In case 1 the result of inner expression (b=!a), should be true if b can be assigned value of !a.
In case 2 the result of inner expression (b==!a), should be true if b is equal to !a.
So what I am trying to say is how does while(b=!a) treated in java?
Does it assign the value to b and check whether b is true or false?
OR
It checks whether the expression (b=!a) was true or not, I mean if there was a condition say (b>a) where b=5 and a is 3 will it return true?
I am a C/C++ background, and I think in C/C++ statement
like while(a=5) will put it in infinite loop.


humm lost to answer here..I'll take a shot at each one..
part of the issue might be the order of prescidence of the operators, ! is higher then =
also remember a while() must evaluate to a boolean it can only result in true or false.
ok while(b=!a) will evaluate to the result of the equasion and set b to the value !a or another way to state it would be while(!a) but then your leaving out the assignment of b.
"(b>a) where b=5 and a is 3 will it return true?" yep that would be true and unless b or a changes, you would get an infinant loop.

while(a=5) : on this it wont compile because a=5 does not evaluate to a boolean, now while(a==5) will evaluate to a boolean the results depending on the value of a.
Does that make more since?
Lee
[This message has been edited by Lee Clarke (edited February 02, 2001).]
[This message has been edited by Lee Clarke (edited February 02, 2001).]
 
Gaurav Mantro
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lee.
You helped me to understand this. Let me try to put down what I understood and just let me know if I got it right
1. While works on logical comparisons since output is always boolean e.g. ==, >, < else.
2. It will also work on assignment operators as long as result operands is boolean. e.g. while(a=true/false) will work and while(a=3) will not work.
Thanks for your time and patience to help me understand this, but it really helped me to clarify a basic thing.
 
Lee Clarke
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to help Gaurav!
I can understand how confusing these simple things can be since they work a bit diferently then other languages. I think explaining it even made it more solid on my end! good rule if you think you understand it try explaining it! )

Good luck!
Lee
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic