Conditional statements in Java are used for decision making.

They consist of condition(s) to be evaluated and the action(s) to be taken based on the outcome of the evaluation.

The action in this case is simply a statement or a set of statements.

The evaluation of the condition(s) returns a Boolean value, either true or false.

In this article, we will discuss the two major Java conditionals, the if and switch statements.

If statements in Java

The Java if statement tests a condition and executes a statement or a set of statements if the outcome of the evaluation is true.

It takes the following syntax:

if(condition) {
  // Statement(s)
}

The if statement doesn’t specify what should be done if the result of evaluation of the condition is false.

For Example:

public class IfExample {
  public static void main(String[] args) {
    if(2>1){
      System.out.print("2 is greater than 1");
    }
  }
}

The code will return the following:

2 is greater than one!

In the above example, we have created a condition to check whether 2 is greater than 1.

Since this is true, the statement within the body of the if statement was executed.

Let us modify the example as follows:

public class IfExample {
  public static void main(String[] args) {
    if(2<1){
      System.out.print("2 is greater than 1");
    }
  }
}

The above code will return nothing when executed.

We have created a condition that checks whether 2 is less than 1.

This is false, hence, the body of the if statement was not executed.
The if statement can be combined with an optional else part that states what should be done when the condition is false.

The following syntax should be used:

if(condition) {
  // Statement(S) to run if condition is true
}else {
  // Statement(s) to run if condition is false
}

For example:

public class IfElseExample {
  public static void main(String[] args) {
    int age = 15;
    if(age>18){
      System.out.print("You are above 18 years!");
    } else{
      System.out.print("You are below 18 years!");
    }
  }
}

The code will return the following:

You are below 18 years!

We created a variable named age and assigned it a value of 15.

We created a condition in the if statement to check whether the value of this variable is greater than 18. This is false.

The statement within the body of if was skipped and the statement within the body of the else was executed.

In the above examples, we have been testing only one condition.  Sometimes, you may need to test more than one conditions.

You can use an advanced form of the if statement for this.

You test the first condition using an if statement, the subsequent ones using else if statements and the last statement using an else statement.

This results into the if…else if…else statement. It takes the following syntax:

if(condition 1) {
  // Statement(s)
}else if(condition 2) {
  // Statement(s)
}else if(condition 3) {
  // Statement(s)
}else {
  // Statement(s)
}

The else part will be executed if none of the conditions evaluates to a true. For example:

public class IfElseEfExample {
  public static void main(String[] args) {
    int age = 15;
    if(age>20){
      System.out.print("You are above 18 years!");
    } else if(age == 15){
      System.out.print("You are 15 years old!");
    } else if(age==20){
      System.out.print("You are 20 years old!");
    } else{
      System.out.print("None of the above!");
    }
  }
}

The code will return the following:

You are 15 years old!

The value of variable age was initialized to 15, hence the test condition for 15 evaluated to a true.

The Java switch statement

With the Java switch statement, we can select the statement to be executed from a set of code blocks.

It tests the equality of a value against a set of other values. It is compatible with byte, int, short, long, String, enum types and wrapper data types such as Byte, Int, Short, and Long.

The Java switch statement takes the following syntax:

switch(expression) {
  case value:
    // Statement(s)
    break; //optional
  case value:
    // Statement(s)
    break; //optional
  default : //Optional
    // Statement(s)
}

The switch will terminate after reaching the break statement.

The flow will then jump to the next line after the switch block.

However, note that it’s not a must for you to add a break to every case.

A default case may be added to the end of the switch to run if none of the cases is true.

For example:

public class SwitchExample {

  public static void main(String args[]) {
    String name = "Leah";

    switch(name) {
      case "Cate" :
        System.out.println("Hello cate!");
        break;
      case "Leah" :
        System.out.println("Hello Leah!");
        break;
      case "Bush" :
        System.out.println("Hello Bush!");
      default :
        System.out.println("Unknown name!");
    }
    System.out.println("Your name is " + name);
  }
}

This will return the following:

Hello Leah!
Your name is Leah!

We created a string variable named name.

It was assigned a value of Leah.

We have then tested the equality of this value using various cases.

The case expression for Leah was matched successfully or evaluated to a true.

The statement below it was executed to return the above.

The last println statement will be executed in all cases regardless of the value of the name.

Happy programming!