Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

logical operator doubt

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
question 1::::

class maybe{
public static void main(String []args)
{boolean b1=true;
boolean b2=false;
System.out.print(!false ^ false);
System.out.print(" " + (!b1 & (b2=true)));
System.out.println(" " + (b2 ^ b1));
}}

doubt::: in the 3rd println how can the output be false?in fact what the ^ operator does is check for only one true in the expression and it gets it from the b1 variable...?

question 2::::also im still not at clear with the & and | operators.

question 3:::could you please explain me the following code

class titanic{
public static void main(String []args){
Boolean b1=true;
Boolean b2=false;
Boolean b3=true;
if((b1 & b2) | (b2 & b3) & b3)
System.out.println("alpha");
if((b1=false) | (b1 & b3) | (b1 | b2))
System.out.println("beta");
}}
the output comes out to be nothing at all. please explain it step by step?

THANKS!!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the confusion comes from mistaking the assignment operator ( = ) for the comparison operator ( == ). For example, b2=true assigns true to b2.

See if that clears things up.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class maybe{
public static void main(String []args)
{boolean b1=true;
boolean b2=false;
System.out.print(!false ^ false);
System.out.print(" " + (!b1 & (b2=true)));
System.out.println(" " + (b2 ^ b1));
}}

doubt::: in the 3rd println how can the output be false?in fact what the ^ operator does is check for only one true in the expression and it gets it from the b1 variable...?

question 2::::also im still not at clear with the & and | operators.


& and | always executes both conditions whatever is the output of 1st conditions. In & operation, eventhough 1st condition is false it 'll execute 2nd condition and in | operation, eventhough 1st condition is true it 'll execute 2nd condition. Exactly opposite is in Short-circuit &&, ||.

in 2nd print true is assigned to b2 =>> (!b1 & (b2=true))
so in 3rd println b1 and b2 both are true so true ^ true output is false


question 3:::could you please explain me the following code

class titanic{
public static void main(String []args){
Boolean b1=true;
Boolean b2=false;
Boolean b3=true;
if((b1 & b2) | (b2 & b3) & b3)
System.out.println("alpha");
if((b1=false) | (b1 & b3) | (b1 | b2))
System.out.println("beta");
}}
the output comes out to be nothing at all. please explain it step by step?

in 1st if condition
(b1 & b2) = false (b2 & b3) = false
so if( false | false & true) gives you false and so it 'll nt print "alpha"


in 2nd if condition
it assigns false to b1 so now b1 = false
(b1 & b3) = true
(b1 | b2) = false | false = false (since b1=false)
so final condition is if( false | true & false) gives false and it doesn't print "beta"

So no output for 2nd example
please go thru tutorials for if condition and how if handles boolean values.
for Eg:
boolean b = true;
if(b=false).... here it assigns false value to be..
but same is not true for int values like :
int b=0;
if(b=1)... compiler will gv error for this since it expects boolean value...
 
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic