1 / 25

Boolean expressions, part 2 : Logical operators

Learn about compare and logical operators in Java, along with examples and solutions to check if a number is between 10 and 20. Also, explore the priority of logical operators and a programming example of determining leap years.

sherryp
Download Presentation

Boolean expressions, part 2 : Logical operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Boolean expressions, part 2: Logical operators

  2. Previously discussed • Recall that there are 2 types of operators that return a boolean result (true or false): • Compare operators: • A compare operator compares 2 numerical expressions and return a Boolean result. • Example: • the compare operation 3 > 1 returns the Boolean value true

  3. Previously discussed (cont.) • Logical operators: • A logical operator compares 2 Boolean (logical) expressions and return a Boolean result. • Example: • the logical operation true AND false returns the Boolean value false

  4. Logical operators • Logical operators in Java:

  5. The logical AND operator && • The && operatoronly operate on the Boolean valuestrue and false • The results are as follows:

  6. The logical AND operator && (cont.) • Example 1: (3 > 1) && (3 < 5) = true && true = true

  7. The logical AND operator && (cont.) • Example 2: (3 > 1) && (3 > 5) = true && false = false

  8. Example program: test if a number is between 10 and 20 • Problem description: • Write a Java program that reads in a number a • The program prints "yes" when 10 ≤ a ≤ 20 and "no" otherwise.

  9. Example program: test if a number is between 10 and 20 (cont.) • Wrong solution: if ( 10 <= a <= 20 ) System.out.println("yes"); else System.out.println(“no");

  10. Example program: test if a number is between 10 and 20 (cont.) Because 10 <= a <= 20 is evaluated as follows: It is illegal to use the compare <= operator between a Boolean value and a number Expression: 10 <= a <= 20 Operators: <= <= Evaluated as: 10 <= a <= 20 (10 <= a is either true or false) true <= 20 or false <= 20

  11. Example program: test if a number is between 10 and 20 (cont.) • Correct solution: Because only numbers that are between 10 and 20 will satisfy the condition 10 <= a && a <= 20 if ( 10 <= a&&a <= 20 ) System.out.println("yes"); else System.out.println(“no");

  12. Example program: test if a number is between 10 and 20 (cont.) import java.util.Scanner; public class Between01 { public static void main(String[] args) { int a; Scanner in = new Scanner(System.in); // Construct Scanner object a = in.nextInt(); // Read in number into a if ( 10 <= a && a <= 20 ) { System.out.println("Number is between 10 and 20"); } else { System.out.println("Number is NOT between 10 and 20"); } } } • Java program:

  13. Example program: test if a number is between 10 and 20 (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/Between01.java    • How to run the program: • Right click on link and save in a scratch directory • To compile:   javac Between01.java • To run:          java Between01

  14. Priority of the logical operators • Priority ranking of the logical operators against the previously discussed operators:

  15. Priority of the logical operators (cont.) • Reference: http://introcs.cs.princeton.edu/java/11precedence/

  16. Priority of the logical operators (cont.) • Example 1: boolean a; Statement: a = 3 > 1 && 4 < 5 ; Operators in statement: = > && < Executed as follows: a = 3 > 1 && 4 > 5 ; // > and < have highest priority a = true && true ; a = true;

  17. Priority of the logical operators (cont.) • Example 2: boolean a; Statement: a = 3 > 1 && ! 4 < 5 Operators in statement: = > && ! < Executed as follows: a = 3 > 1 && ! 4 < 5 // ! has the highest priority Result: error Logical NOT operator (!) cannot be applied to a number

  18. Priority of the logical operators (cont.) boolean a; Statement: a = 3 > 1 && ! (4 < 5) Operators in statement: = > && ! ( < ) Executed as follows: a = 3 > 1 && ! (4 < 5); // ( ... ) has the highest priority a = 3 > 1 && ! true; // ! has the highest priority now a = 3 > 1 && false; // > has the highest priority now a = true && false; // && has the highest priority now a = false; • Example 3:

  19. Programming example: Leap year using a complicated Boolean expression • Leap year description (Wikipedia): • In the Gregorian calendar, the current standard calendar in most of the world, most years that are evenly divisible by 4 are leap years. • Years that are evenly divisible by 100 are not leap years, unless they are also evenly divisible by 400, in which case they are leap years

  20. Programming example: Leap year using a complicated Boolean expression (cont.) • Constructing the Boolean expression for "leap year": • most years that are evenly divisible by 4 are leap years: Year is leap year if: year % 4 == 0 (divisible by 4)

  21. Programming example: Leap year using a complicated Boolean expression (cont.) • Years that are evenly divisible by 100 are not leap years: Year is leap year if: (year % 4 == 0) && !(year % 100 == 0) divisible by 4 AND not divisible by 100

  22. Programming example: Leap year using a complicated Boolean expression (cont.) • unless they are also evenly divisible by 400, in which case they are leap years Year is leap year if: (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0)

  23. Programming example: Leap year using a complicated Boolean expression (cont.) • Java program: import java.util.Scanner; public class LeapYear02 { public static void main(String[] args) { int year; boolean leap; Scanner in = new Scanner(System.in); // Construct Scanner object

  24. Programming example: Leap year using a complicated Boolean expression (cont.) System.out.print("Enter year:"); year = in.nextInt(); // Read in year if ( (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0) ) { System.out.println("It is a leap year"); } else { System.out.println("It is NOT a leap year"); } } }

  25. Programming example: Leap year using a complicated Boolean expression (cont.) • Example Program: (Demo above code) • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/LeapYear02.java • How to run the program: • Right click on link and save in a scratch directory • To compile:   javac LeapYear02.java • To run:          java LeapYear02

More Related