Boolean expressions, part 1: Compare operators
Download
Report
Transcript Boolean expressions, part 1: Compare operators
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 (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
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
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);
}
}
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
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
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
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 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, negation
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
Right to left
Priority of the compare operators (cont.)
• Reference:
http://introcs.cs.princeton.edu/java/11precedence/
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;
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;
a = true;
// <= has higher priority than =