Conditional Operators
Conditional Operators | Description |
---|---|
== | Is equal to |
> | Is greater than |
>= | Is greater than, or equal to |
< | Is less than |
<= | Is less than, or equal to |
!= | Is not equal to |
Example
- (a)
if x = 10 then {
- (b)
if (x equals 42) {
- (c)
if (x => y) {
- (d)
if [x == 10] {
- (e)
if (x == y) {
Answer
e
Example
Lab Work
- 1.
Define an integer variable and assign value “3” to it.
- 2.
Use an if statement to output “Hello” when the integer variable is assigned number 3.
- 3.
Use an if/else statement to output “Goodbye” when any number other than 3 is assigned to the integer variable.
- 4.Is there anything wrong with the following code?int n = 4;if (n >= 3) {System.out.println("Hello!");}if (n == 4) {System.out.println("Hello again!");}
- 5.Use an if/else statement to implement the following requirements:
Output “less than 3” when the number is smaller than 3
Output “equals 3” when the number is 3
Output “greater than 3” when the number is bigger than 3
- 6.Input an integer number, and then,
Output “The number is greater than 6” when the input number is bigger than 6
Output “The number is smaller than 6” when the input number is smaller than 6
- 7.Explain what the following code snippet is trying to do:Scanner scan = new Scanner(System.in);int n = scan.nextInt();if (n > 6) {if (n > 8) {System.out.println("n is greater than 8");}else {System.out.println("n is greater than 6, but n is smaller than 9");}}
Sometimes you may need to use a logical combination of multiple “true or false” conditions. Let’s introduce another concept here, in terms of “Logical Operators.”
Logical Operators
Math: Logical Operators
&& AND relation
|| OR relation
! NOT relation
A && B indicates only when both A and B are true, the result is true. For example, in (x > 3 && x < 5), A is “x > 3”, B is “x < 5”.
A || B indicates when either A or B is true, the result is true.
!A indicates when A is true, the result is false; when A is false, the result is true. In the example of “!(x > 0)”, A is “x > 0”.
(A && B) | A=true | A=false |
B=true | True | False |
B=false | False | False |
(A || B) | A=true | A=false |
B=true | True | True |
B=false | True | False |
Summary - Result is false, only when both A and B are false. Otherwise, result is true.
(x < 0 || x > 0) (x != 0)
!(x == 0 || y == 0) is equivalent to (x != 0 && y != 0)
!(x > 3 && x < 5) is equivalent to (x >= 5 || x <=3)
Using a Venn diagram will help us analyze some type of logical problems.
Math: Analyzing Logical Problems
A Venn diagram is a visualization method to reveal logical relations among data sets.
In Figure 10-3, the overlap area between circle A and circle C is in area B.
Lab Work
- 1.Figure out the output of the following program.public class LogicalOperation {public static void main(String args[]) {boolean a = true;boolean b = false;System.out.println("a && b = " + (a&&b));System.out.println("a || b = " + (a||b) );System.out.println("!(a && b) = " + !(a && b));}}
- 2.
Write a static method called quadrant that takes as parameters a pair of integer numbers representing an (x, y) point on the Cartesian coordinate system. It returns the quadrant number (i.e., 0,1,2,3,4, see picture) for that point.
Call | Value Returned |
---|---|
quadrant(12, 17) | 1 |
quadrant(-2, 3) | 2 |
quadrant(-15, -3) | 3 |
quadrant(4, -42) | 4 |
quadrant(0, 3) | 0 |
Problems
- 1.Translate the following English statements into logical expressions:
- (a)
z is odd.
- (b)
x is even
- (c)
y is positive.
- (d)
Either x or y is even.
- (e)
y is a multiple of z.
- (f)
z is not zero.
- (g)
y is a positive number, and y is greater in magnitude than z.
- (h)
x and z are of opposite signs.
- (i)
y is a nonnegative one-digit number.
- (j)
z is nonnegative.
- (a)
- 2.
Given the following variable declarations: int x = 4; int y = -3; int z = 4;
What are the results (True or False) of the following expressions?
x == 4 x == y
x == z y == z
x + y > 0 x - z != 0
y * y <= z y / y == 1
x * (y + 2) > y - (y + z) * 2