How to Do Boolean Comparisons in Java
- 1). Add some variables to your program and find out whether one is less than the other. Use the following sample code:
int numA = 5;
int numB = 3;
if(numA<numB) System.out.println("A is less than B");
else System.out.println("A is not less than B");
This code tests whether the first number is less than the other, writing a message if it is. If the first test returns a false result, the code execution will jump to the else section and execute it. The else section does not indicate that the first number is greater than the second, as the numbers may be equal. The following alternative version carries out an additional test:
if(numA<numB) System.out.println("A is less than B");
else if(numA>numB) System.out.println("A is greater than B");
else System.out.println("The numbers are equal");
The final else section in this case will only execute if the first two tests return false results, so the numbers must be equal. - 2). Test whether the numbers are either greater than or equal to one another. The following code demonstrates this shorthand technique:
if(numA>=numB) System.out.println("A is either greater than or equal to B");
To determine whether a number is less than or equal to another, provide an else section, use the following code:
if(numA<=numB) System.out.println("A is either less than or equal to B");
else System.out.println("A is greater than B");
The else section will only execute if the first number is neither equal to nor less than the second. - 3). Find out if a variable is equal to another. If you only need to know whether two values are equal, use the following syntax:
if(numA==numB) System.out.println("A and B are equal");
The double equals sign performs an equality test, not to be confused with the single equals sign, which carries out an assignment. To find out if the two variables are not equal, use the following notation:
if(numA!=numB) System.out.println("A and B are not equal");
The result of this test does not indicate which number is higher or lower, just that the two are not equal. - 4). Chain your tests to satisfy more than one condition. The following sample code demonstrates chaining two conditional tests together:
if(numA>numB && numB>=0) System.out.println("A is greater than B and is also greater than zero, because B is at least zero");
This test will only return a true result if both individual tests also return true result. If either of the two tests returns a false result, the entire statement returns false and its content does not execute. - 5). Chain tests together to specify optional conditions. To test for either of two conditions, use the following code:
if(numA>numB || numA<0) System.out.println("A is greater than B or is less than zero");
This test returns a true result if either of the individual tests returns true. If the first test returns a true result, Java will not even execute the second test, as it will immediately execute the content of the conditional statement, outputting the message. You can chain together as many tests as you like.
Source...