Java Conditional Statements

Java conditional statements are the keywords that execute depending on the conditions defined by programmer. Often there comes a situation where a program needs to take a decision based on the user input or based on some pre defined conditions. In those cases decision making tools are required. Java provides conditional statements that help achieve decision making. The structure of decision making is shown in the diagram below:

Java Conditional Statements
Java Conditional Statements

Java Conditional Statements

Here is a list of Java conditional statements :

  • if statement
  • else statement
  • else if statement
  • switch statement
StatementDescription
if statementIn if statement condition/conditions are tested by a Boolean expression and one or more statements to be executed are followed
if…else statementIn if… else statement condition/conditions are tested by a Boolean expression and one or more statements to be executed if the conditions are true else some other statements are executed i.e. when conditions are false
nested if statementsWhen if statements are used within the block of if statements then it is called nested if statements.
switch statementIn a switch statement, a variable is tested for equality against a list of values.
Java Conditional Statements

The details of each Java Conditional Statement is given in the next sections:

Java if Statement

If statement uses the if keyword followed by parenthesis (), that is followed by curly braces {}. The condition to be verified is written in parenthesis (). The curly braces {} contain the pieces of code. The if statement executes the code present in curly braces {} if the condition is true. If the condition is false then it bypasses the code present in curly braces and exists the if statement.

The syntax of writing if statement is:

The below code shows the working of if statement in Java code:

public class Example {
  public static void main(String[] args) {
    if (10 > 5) {
      System.out.println("10 is greater than 5"); // if statement is true
    } 
    System.out.println("This statements executes either if statement is true or false");
  }
}

The output of the above code after compilation is:

Here is another example that shows what happens if the if condition is false. In this code only the if condition is changed to (10 < 5), that is false. So, in this case the code in curly braces is by passed and compiler jumps outside the if statement.

public class Example {
  public static void main(String[] args) {
    if (10 < 5) {
      System.out.println("10 is greater than 5"); // if statement is false
    } 
    System.out.println("This statements executes either if statement is true or false");
  }
}

The output of the above code after compilation is:

Java if…else Statement

if…else condition uses if and else keywords. The if else statement executes the code present in curly braces {} if the condition in if is true. If the condition is false, it executes the code present in curly braces {} after the else keyword. In any case either of the two statements will be executed. The syntax for the if else statement is as follows:

The below example, demonstrates the working of if…else statement.

public class Example {

   public static void main(String args[]) {
      int x = 10;

      if( x < 5 ) {
         System.out.print("if statement is executed");
      }
      else {
         System.out.print("else statement is executed");
      }
   }
}

Here, the value of x is 10 and since it greater than 5 so the condition in if parenthesis becomes false. The compiler skips the code in if statement and executes the code in else statement. The output of the above code after compilation is:

Here is another example, that shows what happens if the condition in if statement is true. The value of variable x is changed to 2.

public class Example {

   public static void main(String args[]) {
      int x = 2;

      if( x < 5 ) {
         System.out.print("if statement is executed");
      }
      else {
         System.out.print("else statement is executed");
      }
   }
}

Since 2 is smaller than 5, so the if condition becomes true and the code in if statement will be executed. The output of the above code after compilation is:

Java Nested if Statement

Java Nested if statement uses multiple if keywords. Nested if statements have another if statement within an if statement. If the condition in the first if statement is true, then the second if statement is executed. But if the first if condition is false, the second if statement is not executed whether it is true or false.

The syntax for the nested if statement is as follows:

For example, the below code shows the working of nested if statement:

public class Example {

   public static void main(String args[]) {
      int x = 10;
      int y = 20;

      if( x < 20 ) {
         if( y < 30 ) {
            System.out.print("X = " + x + " and Y = " + y);
         }
      }
   }
}

Since both if values are true so the statement is executed. The output of the above code after compilation is:

Java Switch Statement

Java Switch Statement checks value of a variable against a number of values. The list of values is called cases. It uses switch, case and break keywords. When we have multiple possible cases to handle, we use switch statements. User can add multiple cases under the switch statement. Among all the cases, only one case’s value matches the value of the expression. The code associated to the case whose value equals the expression is executed. The syntax for the switch statement is as follows:

The variable present in a switch statement can be of following data types:

  • int
  • byte
  • short
  • char
  • string
  • enum

Using break keyword is optional. User can define multiple cases. The default statement is also optional. It is executed in case the variable value matches with no case value. The default statement must be added at the end and it does not require break statement. The below example shows the working of switch statement:

public class Example {

   public static void main(String args[]) {
      char grade = 'C';

      switch(grade) {
         case 'A' :
            System.out.println("Marks above 90"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Marks above 80");
            break;
         case 'D' :
            System.out.println("Marks above 60");
         case 'F' :
            System.out.println("Marks below 50");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

The output of the above code after compilation is: