### Boolean expressions, part 1: Compare operators

• Compare operators

• 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

• 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 equal to b, otherwise returns false
!=   a != b   Returns true if a is not equal to b, otherwise returns false

• Example program: test divisibility

• Problem description:

 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

• Algorithm:

• A number a is divisible by the number b if and only if:

 The remainder of the division a/b is equal to 0

• 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); } } ```

Explanation:

• The expression (a % b) == 0 will:

 First compute the remainder of the division a/b Then compare the result (i.e., the remainder of the division) to the value 0

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Divisible01.java To run:          java Divisible01

• 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: click here

• 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

 ``` a < b == c ```

Because you cannot have back to back compare operations, there is no need to decide which one has higher priority.

• Priority ranking of the compare operators against the previously discussed operators:

Priority level     Operator(s)     Description     Associativity
1  ( )    Brackets
2  (int)   −    Casting, negation   Right 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

• Example 1:

 ``` boolean a; Statement: a = 3 > 1; Operators in statement: = > Executed as follows: a = 3 > 1; // > has higher priority than = a = true; ```

• 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 = false; ```

• Commonly made mistakes with compare operators

• Mistake 1: using to compare equality

Example:

 ``` if ( a = 0 ) { ... } else { ... } ```

The = symbol represents the assignment operator

You must use == to compare equality (no spaces between the == characters)