What will the code below print out? Try to guess before you run it! Note that 1 equal sign (=) is used for assigning a value and 2 equal signs (==) for testing values.
The Relational Operators below in Java are used to compare numeric values or arithmetic expressions. Although some programming languages allow using relational operators like < to compare strings, Java only uses these operators for numbers, and uses the string methods compareTo() and equals() for comparing String values.
If you have trouble telling < and > apart, think of a number line and think of < and > as arrows; < (less than) points towards 0 and smaller numbers on the number line and > (greater than) points towards the larger numbers on the number line. Or remember that < starts with the smaller (less) point and > starts with the open wide (greater) side. With <= and >=, remember to write the two symbols in the order that you would say them “less than” followed by “or equal to”.
// Test if a number is positive
(number > 0)
//Test if a number is negative
(number < 0)
//Test if a number is even by seeing if the remainder is 0 when divided by 2
(number % 2 == 0)
//Test if a number is odd by seeing if there is a remainder when divided by 2
(number % 2 > 0)
//Test if a number is a multiple of x (or divisible by x with no remainder)
(number % x == 0)
Checkpoint2.7.5.
Try the expressions containing the % operator below to see how they can be used to check for even or odd numbers. All even numbers are divisible (with no remainder) by 2.
The modulo operator is a powerful tool in programming. Because of its versatility, it’s important to become familiar with how it works and how it can be applied in different problem-solving scenarios
Use it to check for odd or even numbers (num % 2 == 1) is odd and (num % 2 == 0) is even. Actually, you can use it to check if any number is evenly divisible by another (num1 % num2 == 0)