Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Interesting fact about AND and OR operator

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 380
    Comment on it

    Hello readers, 

    I want to share an interesting fact about AND and OR operator, which you might not had encountered. As we know that in AND operator the result is only true when the values of the operands are true, if any of the operand is false then the value returned by the AND operation on then will be false. On the other hand, OR returns true if any of its operand is true, false will only be returned if and only if both the operands have false value.

     

    Truth tables for both of then given here.

     

    For AND operator                  For OR operator

                          

     

    So from above we can conclude that if left operand for AND operator is false ( 0 ) then out will be 0 or false, and if left operand is true then we also need to check the right operand.

    And in case of OR if left operator is true or 1 then output will only be 1 or true else if it is 0 or false then right operand should be computed for the final value to be returned by OR operator.

     

    Sample program to check the functionality of AND and OR operator

     

    public class OperatorDemo {
    	public static boolean fun1() {
    		System.out.println("Call to function fun1 ");
    		return true;
    	}
    	
    	public static boolean fun2() {
    		System.out.println("Call to function fun2 ");
    		return true;
    	}
    	
    	public static boolean fun3() {
    		System.out.println("Call to function fun3 ");
    		return false;
    	}
    	
    	public static boolean fun4() {
    		System.out.println("Call to function fun4 ");
    		return true;
    	}
    	
    	public static void main(String[] args) {
    		
    		if (fun1() || fun2()) {
    			System.out.println("if condition using OR operator is true");
    		} else  {
    			System.out.println("if condition using OR operator is false");
    		}
    		
    		if (fun3() && fun4()) {
    			System.out.println("if condition Using AND operator is true");
    		} else  {
    			System.out.println("if condition Using AND operator is false");
    		}
    	}
    }

    Output of the above code is

    Call to function fun1 
    if condition using OR operator is true
    Call to function fun3 
    if condition Using AND operator is false

     

    Here in output we can see that fun2() and fun4() were not called, because of the above discussed scenario. Because of this property they are also known as short-Circuit logical operators.

    Hope you like it.

    Thanks, 

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: