Difference between logical operators and or Java

Basic difference remember it!!!
if(TRUE && TRUE && TRUE) return TRUE otherwise FALSE

if(FALSE || FALSE || FALSE) return FALSE otherwise TRUE

Logical operator and &&
If all conditions/operands is TRUE return TRUE, otherwise return FALSE

if( true and true and true){ 
  // return true - do something
}
int a = 6;
if(a == 6 && a == 6 ) {
 // if TRUE
 // true && true return true, do something
}
 
if(a == 6 && a == 5){
  // nothing, not attended
}else{
  // true && false return false, or false && false return false 
  // do something
}

Logical operator or ||
The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise.
If one operands is TRUE, condition is TRUE:

if(FALSE OR FALSE OR TRUE) return TRUE
if(FALSE OR TRUE OR FALSE) return TRUE
if(FALSE OR FALSE OR FALSE) return FALSE
 
int a = 6;
if(a==6 || a==5){ // TRUE || FALSE return TRUE
  //if return TRUE
  //one from operadns is TRUE return true,  do something
}
 
if(a==5 || a==4){ // FALSE || FALSE return FALSE
 // not attended
}else{
 //if return FALSE, do something
}

rev

This entry was posted in Android Examples Code and tagged , . Bookmark the permalink.


Comments are closed.

6,525 views