In the previous lesson you learnt about using enum for constants.


Logical operators and the if statement

Frequently in programming you need to check whether a certain condition is true and perform different processing if the condition holds to when the condition does not hold. Java provides the if statement to help you achieve this:

There are several logical operators used primarily to compare primitives:.

== Equal to (note the double equal signs)
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

The comparison operator from the above table to take note of is ==, where there are two consecutive equal symbols, and which means to check whether one value is equal to another value. A common mistake among beginners is to confuse this with the single equals operator = you used previously, and which means assignment.

  • Single equals sign means assignment
  • Double equals sign means comparison

The Java if statement is used to test whether a particular condition holds, where the conditional expression resolves to a boolean value of either true or false:

// Using if for conditional statements
int a = 3;
int b = 4;

if (a == b) {
    System.out.println("a is equal to b");
}

if (a != b) {
    System.out.println("a is not equal to b");
}

if (a < b) {
    System.out.println("a is less than b");
}

if (a <= b) {
    System.out.println("a is less than or equal to b");
}

if (a > b) {
    System.out.println("a is greater than b");
}

if (a >= b) {
    System.out.println("a is greater than or equal to b");
}
 

Braces are used to demarcate a block of code that will be executed if the condition is met. In the above example, because variable a is 3 and variable b is 4, the three blocks will be executed because the if conditions return true:

  • a is not equal to b: (a != b);
  • a is less than b: (a < b);
  • a is less than or equal to b: (a <= b).

The other if conditions will return false and therefore the code inside those blocks will not be executed.

The above operators will work for any numeric primitive type: byte, short, int, long, float, double and char.

If your if condition is comparing two booleans you can only use == and !=:

// Using if with booleans
boolean sunny = true;
boolean raining = false;

if (sunny == true) {
    System.out.println("It is sunny");
}

if (sunny == false) {
    System.out.println("It is not sunny");
}

if (sunny != true) {
    System.out.println("It is not sunny");
}

if (sunny != false) {
    System.out.println("It is sunny");
}

if (sunny == raining) {
    System.out.println("It is either sunny and raining" +
                         " or not sunny and not raining");
}

if (sunny != raining) {
    System.out.println("It is either sunny and not raining" +
                         " or not sunny and is raining");
}
 

If you only need to execute one statement when the condition is met, then the braces are optional:

if (sunny == true) System.out.println("It is sunny");

However, for clarity it is suggested that you always include the braces:

if (sunny == true) {

    System.out.println("It is sunny");

}

You can abbreviate boolean comparisons to remove the second operand, for example:

if (sunny) 

Is equivalent to saying:

if (sunny == true) 

You can use the ! (read as "not") symbol to negate the comparison, hence:

if (! sunny) 

Is equivalent to saying:

if (sunny == false) 

Examples in use:

// if statement with implied test for true
if (sunny) {
    System.out.println("It is sunny");
}

// if statement with implied test for false
if (! sunny) {
    System.out.println("It is not sunny");
}
 

In the next lesson you will learn about compound operators.

Lesson 5.6 Compound operators