Download presentation
Presentation is loading. Please wait.
Published byVanessa Gibbs Modified over 9 years ago
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: Operator symbol Meaning Comment &&The logical AND operator Binary operator ||The logical OR operator Binary operator !The logical NOT operator Unary operator
5
The logical AND operator && The && operator only operate on the Boolean values true and false The results are as follows: Operation Result true && true true true && false false false && true false false && false false
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.) Java program: 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"); }
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: Priority level Operator(s)Description Associativity 1 ( ) Brackets 2(int) − !Casting, negation, logical NOT Right to left 3++, -- Increment, decrement 4* / % Multiple, divide, remainder Left to right 5+ - Add, subtract Left to right 6 >= == != Compare operators 7 &&logical AND Left to right 8||logical OR Left to right 9= += -=...Assignment operators Right to left
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.) Example 3: 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;
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.