Java If ... Else


Java Conditions and If Statements

Java supports common logical conditions from mathematics

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these terms to perform different actions in different decisions.

Java has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the statement if you want to specify a Java code block to use if the condition is true.


Syntax
if (condition) {
            // block of code to be executed if the condition is true
          }
          

Note that if is written in small capital letters. Capitalized characters (If or IF) will create an error.


In the example below, we check two numbers to see if 20 is greater than 18. If the situation is true, print some text:


Example
if (20 > 18) {
            System.out.println("20 is greater than 18");
          }
          

We can also check the variables:


Example
int x = 20;
            int y = 18;
            if (x > y) {
              System.out.println("x is greater than y");
            }
            

Example Explained

In the example above we use two variables, x and y, to test whether x is greater than y (using > operator). Since x is 20, and y is 18, and we know that 20 is greater than 18, we print on the screen that "x is greater than y".


The else Statement

Use else statement to specify a code block to use if the situation is false.


Syntax
if (condition) {
            // block of code to be executed if the condition is true
          } else {
            // block of code to be executed if the condition is false
          }
          


Example
int time = 20;
            if (time < 18) {
              System.out.println("Good day.");
            } else {
              System.out.println("Good evening.");
            }
            // Outputs "Good evening."
            

Example Explained

In the example above, the time (20) is greater than 18, so the situation is false. As a result, we move on to else condition and print on the "Good evening" screen. If the time was less than 18, the program would print "Good Day".


The else if Statement

Use else if the statement specifies the new condition if the original condition is false.


Syntax
if (condition1) {
            // block of code to be executed if condition1 is true
          } else if (condition2) {
            // block of code to be executed if the condition1 is false and condition2 is true
          } else {
            // block of code to be executed if the condition1 is false and condition2 is false
          }
          


Example
int time = 22;
            if (time < 10) {
              System.out.println("Good morning.");
            } else if (time < 20) {
              System.out.println("Good day.");
            } else {
              System.out.println("Good evening.");
            }
            // Outputs "Good evening."
            

Example Explained

In the example above, the time (22) is greater than 10, so the initial condition is false. The next situation, in else case the statement, is also false, so we move on to another situation as condition1 and condition2 are both false - and print on the "Hello" screen.

However, if the time was 14, our program would print "Good Day."


Short Hand If...Else (Ternary Operator)

There is also a short-hand alternative, known as a ternary operator because it contains three operands. It can be used instead of multiple lines of code in one line. It is often used for a simple replacement if other statements:


Syntax
variable = (condition) ? expressionTrue :  expressionFalse;
        

Instead of writing:


Example
int time = 20;
            if (time < 18) {
              System.out.println("Good day.");
            } else {
              System.out.println("Good evening.");
            }
            

You can easily write:


Example
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);