- Home
- Documents
*Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types...*

prev

next

of 25

View

212Download

0

Embed Size (px)

Boolean expressions, part 2: Logical operators

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

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

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

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

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

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

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.

- Example program: test if a number is between 10 and 20 (cont.)Wrong solution: if ( 10
- Example program: test if a number is between 10 and 20 (cont.)Because 10
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

- 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
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

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

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

Priority of the logical operators (cont.)Example 1: boolean a;

Statement: a = 3 > 1 && 4 < 5 ;

Operators in statement: = > && 1 && 4 > 5 ; // > and < have highest priority

a = true && true ;

a = true;

Priority of the logical operators (cont.)Example 2: boolean a;

Statement: a = 3 > 1 && ! 4 < 5

Operators in statement: = > && ! 1 && ! 4 < 5 // ! has the highest priority

Result: error Logical NOT operator (!) cannot be applied to a number

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;

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

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)

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

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)

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

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"); } } }

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