Logical operators

  • Logical operators:

    • Are operators that perform computation on logical values

    • Are used to combine simple conditions to make more complex conditions

  • The logical operators in Java are:

     Symbol   Oper Name      Effect
     ------   ---------      -------------------------------
       !      not-operator   inverts a condition
    
       &&     and-operator   make sure that BOTH conditions are true
    
       ||     or-operator    make sure that at least ONE of 
                             the conditions is true
    
       ^      xor-operator   make sure that exactly ONE of 
                             the conditions is true  

The ! (not) operation

  • The ! (not) operator inverts a condition:

      condition   !condition     Example: check radius is not negative 
     ------------------------------------------------------------------
      true        false          !(radius < 0)
      false       true           
    
      

  • Example: compute the area for circles with a radius that is not negative

      if ( !(radius < 0) )  // If radius is NOT negative
         area = 3.14159*radius*radius;
      

DEMO: demo/03-selections/06-logic-ops/NotOp.java

The && (and) operation

  • The && (and) operator makes sure that 2 conditions are both true:

      cond1  cond2  cond1&&cond2   Example: test 0 <= radius <= 20 
     ---------------------------------------------------------------
      true   true   true           (radius >= 0) && (radius <= 20)  
      true   false  false
      false  true   false
      false  false  false

  • Example: compute the area for circles with a radius between [0..20]

      if ( (radius >= 0) && (radius <= 20) )
         area = 3.14159*radius*radius; 

DEMO: demo/03-selections/06-logic-ops/AndOp.java

The || (or) operation

  • The || (or) operator makes sure that at least ONE of 2 conditions are true:

      cond1  cond2  cond1||cond2   Eg: Test if x is div by 2 or 3
     ---------------------------------------------------------------
      true   true   true           (x%2 == 0) || (x%3 == 0)  
      true   false  true 
      false  true   true 
      false  false  false

  • Example: print message if the number x is divisible by 2 or by 3

      if ( (x%2 == 0) || (x%3 == 0) )
         System.out.println("number is divisible by 2 or 3"); 

DEMO: demo/03-selections/06-logic-ops/OrOp.java

The ^ (xor) operation

  • The ^ (xor) operator makes sure that exactly ONE of 2 conditions are true:

      cond1  cond2  cond1^cond2   Eg: is x is div by either 2 or 3
     --------------------------------------------------------------
      true   true   false          (x%2 == 0) ^ (x%3 == 0)  
      true   false  true 
      false  true   true 
      false  false  false

  • Example: print message if the number x is divisible either by 2 or by 3 (but not both)

      if ( (x%2 == 0) ^ (x%3 == 0) )
      {
         System.out.println("number is divisible either by 2 or 3");
         System.out.println("but not both");
      }

DEMO: demo/03-selections/06-logic-ops/XorOp.java

Sample program    check if a number is divisible by 2 and by 3 indivdually

Write a program that read in a number and print whether it is divisible by 2 and by 3 individually

import java.util.Scanner;

public class Div1 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int x = input.nextInt();
       
       if ( x%2 == 0 )
          System.out.println("is divisible by 2");
       else
          System.out.println("is not divisible by 2");

       if ( x%3 == 0 )
          System.out.println("is divisible by 3");
       else
          System.out.println("is not divisible by 3");
   }
}  

DEMO: demo/03-selections/06-logic-ops/Div1.java

Sample program    check if a number is divisible by 2 and by 3

Write a program that read in a number and print whether it is divisible by 2 and by 3

import java.util.Scanner;

public class Div2 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int x = input.nextInt();
       
       if ( (x%2 == 0) && (x%3 == 0) )
          System.out.println("is divisible by BOTH 2 and by 3");
       else
          System.out.println("is NOT divisible by BOTH 2 and by 3");
   }
}  

DEMO: demo/03-selections/06-logic-ops/Div2.java

Sample program    check if a number is divisible by 2 or by 3

Write a program that read in a number and print whether it is divisible by 2 or by 3

import java.util.Scanner;

public class Div3 
{
   public static void main(String[] args) 
   {
       Scanner input = new Scanner(System.in);
       
       int x = input.nextInt();
       
       if ( (x%2 == 0) || (x%3 == 0) )
          System.out.println("is divisible by 2 OR by 3");
       else
          System.out.println("is NOT divisible by 2 NOR by 3");
   }
}  

DEMO: demo/03-selections/06-logic-ops/Div3.java

Caution:    Math notation vs. Java notation

 

  • Math notation for:   "x is between 1 and 10":

              1 <= x <= 10      

  • Java notation for:   "x is between 1 and 10":

              1 <= x  &&  x <= 10