Download presentation
Presentation is loading. Please wait.
Published byDorothy Glenn Modified over 8 years ago
1
Boolean expressions, part 1: Compare operators
2
Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A compare operator will return the value true if the test is successful A compare operator will return the value false if the test is unsuccessful
3
Compare operators (cont.) Compare operators in Java: Operator symbol Example Meaning <a < b Returns true if a < b, otherwise returns false <=a <= b Returns true if a ≤ b, otherwise returns false >a > b Returns true if a > b, otherwise returns false >=a >= b Returns true if a ≥ b, otherwise returns false ==a == b Returns true if a is not equal to b, otherwise returns false !=a != b Returns true if a is not equal to b, otherwise returns false
4
Example program: test divisibility Problem description: Algorithm: Write a Java program that reads in a number a and a number b The program print a message when a is divisible by b A number a is divisible by the number b if and only if: The remainder of the division a/b is equal to 0
5
Example program: test divisibility (cont.) Java program: import java.util.Scanner; public class Divisible { public static void main(String[] args) { int a, b; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter a: "); a = in.nextInt(); // Read in number into a System.out.print("Enter b: "); b = in.nextInt(); // Read in number into b if ( (a % b) == 0 ) System.out.println(a + " is divisible by " + b); }
6
Example program: test divisibility (cont.) Explanation: The expression (a % b) == 0 will: 1.First compute the remainder of the division a/b 2.Then compare the result (i.e., the remainder of the division) to the value 0
7
Example program: test divisibility (cont.) Example Program: (Demo above code) –Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/06/Progs/ Divisible01.java How to run the program: Right click on link and save in a scratch directory To compile: javac Divisible01.java To run: java Divisible01
8
Comparing integer and floating point values Automatic conversion rule for compare operators: The same automatic conversion rules used for arithmetic operators apply for compare operators The automatic conversion rules for arithmetic operators were summarized on this webpage: http://mathcs.emory.edu/~cheung/Courses/170/S yllabus/04/conversion.html
9
Priority of the compare operators For practical purposes, you can assume that: All compare operators have the same priority This is because you cannot have back to back compare operations Example: this is illegal Because you cannot have back to back compare operations, there is no need to decide which one has higher priority. a < b == c
10
Priority of the compare operators (cont.) Priority ranking of the compare operators against the previously discussed operators: Priority level Operator(s) Description Associativity 1( ) Brackets 2(int) − Casting, negationRight to left 3++, -- Increment, decrement 4* / % Multiple, divide, remainder Left to right 5+ - Add, subtract Left to right 6 >= == != Compare operators 7 = += -=...Assignment operators Right to left
11
Priority of the compare operators (cont.) Reference: http://introcs.cs.princeton.edu/java/11precedence/
12
Priority of the compare operators (cont.) Example 1: boolean a; Statement: a = 3 > 1; Operators in statement: = > Executed as follows: a = 3 > 1; // > has higher priority than = a = true;
13
Priority of the compare operators (cont.) Example 2: boolean a; Statement: a = 3 + 4 <= 5 - 2; Operators in statement: = + <= - Executed as follows: a = 3 + 4 <= 5 - 2; // + and - has highest priority a = 7 <= 3; // <= has higher priority than = a = true;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.