• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

logical operator doubt

 
aditi mudholkar
Greenhorn
Posts: 7
  • 0
  • 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!!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • 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.
 
m Sabale
Greenhorn
Posts: 5
  • 0
  • 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...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic