If Else Statement in Java

On this page, explore the if-else statement in Java programming, covering the If statement, the If-else statement, the Else-if statement, and the nested if-else statement. Gain insights into syntax, flowcharts, and examples for each type of if-else statement in Java.


What is the if-else statement in Java programming?

Explore the pivotal if-else statement in Java, a cornerstone of control flow. This fundamental structure empowers programs to make decisions based on conditions. In Java, the if-else statement executes a designated block of code if a condition holds true, and an alternative block if the condition is false. Dive into the essence of decision-making in Java programming with this essential control flow feature.

There are four types of if-else statements in Java programming:

  1. If statement in Java
  2. If-else statement in Java
  3. Else-if statement in Java
  4. Nested if-else statement in Java


1. If statement in Java:

The if statement represents the fundamental cornerstone of conditional control in Java. This essential construct enables the evaluation of a single condition, executing the enclosed code only when the condition holds true. If the condition evaluates to false, the code within the if statement gracefully bypasses execution.


Syntax of if statement in c:


  if (condition) {
    // code to execute if condition is true
  }


if statement workflow:

if condition in Java.


Example of if statement in c:


  import java.io.*;
  public class Main {
    public static void main(String[] args) {
      // Assign a value to x
      int x = 10;

      // Check if x is greater than 5
      if (x > 5) {
          System.out.println("x is greater than 5");
      }
    }
  }

Output:


  x is greater than 5


2. If-else statement in Java:

The if-else statement in Java facilitates the execution of distinct code paths based on the result of a single condition. When the condition evaluates to true, the code within the if block is executed; otherwise, if the condition is false, the code within the else block takes precedence.

Syntax of if-else statement in Java:


  if (condition){
    //code to execute if condition is true
  }
  else{
    //code to execute if condition is false
  }


if-else statement workflow:

if-else example in Java.



Example 1 of if-else statement:



  import java.io.*;
  public class Main {
    public static void main(String[] args) {
      int a = 5, b = 3;
      
      // Check if a is greater than b
      if (a > b) {
          System.out.println("a is greater than b");
      } else {
          System.out.println("b is greater than a");
      }
    }
  }

Output:


  a is grater then b



Example 2 of if-else statement:



  import java.io.*;
  public class Main {
    public static void main(String[] args) throws IOException {
      int a = 2, b = 3;

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


Output:

  
  b is grater then a


3. Else-if statement in Java:

The else-if statement, often referred to as the else-if ladder, extends the functionality of the basic if-else statement in Java programming. This construct empowers developers to assess multiple conditions and execute distinct statements based on the outcomes of these conditions, offering a versatile approach to decision-making in code.

The syntax for the else-if statement is:


  if (condition1) {
    // code to be executed if condition1 is true
  } 
  else if (condition3) {
    // code to be executed if condition3 is true
  } 
  else {
    // code to be executed if none of the conditions are true
  }

In Java, the else-if statement streamlines the process of checking multiple conditions in a structured manner. It begins by evaluating condition1; if true, the corresponding block of code is executed. If false, it proceeds to condition2, and the process repeats. This sequence continues until a true condition is found, executing the associated code block. If none of the conditions are met, the code within the final else block is executed. This approach enhances code readability and efficiency, providing a concise way to handle complex conditional scenarios in Java.

Workflow else-if statement or if-else ladder in Java:


else if in in Java.


Example of else-if statement or if-else ladder in Java:



  import java.util.Scanner;
  public class GradeCalculator {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);

      System.out.print("Enter the score: ");
      int score = scanner.nextInt();
      char grade;

      if (score >= 90 && score <= 100) {
        grade = 'A';
      } else if (score >= 80 && score <= 89) {
        grade = 'B';
      } else if (score >= 70 && score <= 79) {
        grade = 'C';
      } else if (score >= 60 && score <= 69) {
        grade = 'D';
      } else {
        grade = 'F';
      }
      System.out.println("Your grade is " + grade);
      scanner.close();
    }
  }

Output:


  Enter the score: 90
  Your grade is A


4. Nested if-else statement :

Nested if-else statements in C programming refer to the usage of if-else statements inside another if-else statement. This means that you can have multiple if-else statements inside each other to make complex decisions in your program.


Syntax of nested if-else statement in C:



  if(condition1){
    //code to be executed if condition1 is true
    if(condition2){
      //code to be executed if both condition1 and condition2 are true
    }
    else{
      //code to be executed if condition1 is true and condition2 is false
    }
  }
  else{
    //code to be executed if condition1 is false
  }


In the given example, when condition1 is true, the code within the initial if block is executed. If condition2 is also true, the code within the nested if block is executed; otherwise, the code inside the else block of the nested if-else statement takes precedence. In the scenario where condition1 is false, the code within the else block of the outer if-else statement is executed.

It's crucial to highlight that each variant of the if-else statement in Java can be nested within another, offering the flexibility to construct intricate and layered conditional statements. This nesting capability allows programmers to build sophisticated decision-making structures to address diverse and complex scenarios in their code.

Flowchart of nested if-else statement in C:


Flowchart of nested if else statement in java

An example of nested if-else statement in Java:



  import java.io.*;
  public class NestedIfExample {
    public static void main(String[] args) {
      int num1 = 10;
      int num2 = 20;

      if (num1 == num2) {
        System.out.println("The numbers are equal.");
      } else {
        if (num1 > num2) {
          System.out.println("Num1 is greater than num2.");
        } else {
          System.out.println("Num2 is greater than num1.");
        }
      }
    }
  }

Output:


  Num2 is greater than num1.