If else in Java


JavaViews 2768

Whenever we write any Java Program, we may have to include many decision-making statements such as If, If Else, etc. In this tutorial, we will learn about If Else statements in Java. We can also name them as Java Conditional statements.

If else in Java works based on a certain condition. When the condition is satisfied, it executes code inside if statement, and when false, it executes the code inside else condition. The conditions can be defined using logical operators (>, >=, <, <=, !=, ==) .

If else code blocks are enclosed within curly braces {}. If there is only 1 statement, then we can omit the braces.

If else keywords in Java are case sensitive and both should be in lower case. (if and else)

There are different types of If else in Java:

  • If statement
  • If else statement
  • Nested if statement
  • If else if statement

Java If Statement

If statement contains a condition or expression. When the condition is true, then the corresponding block of code inside if statement gets executed. If the expression is false, then it ignores this code block. Since the statement is executed based on a condition, we call it as conditional statements. This is one of the types of if else in Java where there is only if condition.

Syntax

if(condition) {
   //executes this code when condition is true
}

//If there is only 1 statement within if block
if(condition)
   //execute this line

Flowchart

If Else in Java

Example of If Statement

In the below example, we have 2 variables a and b with values 10 and 20. Since 10 < 20 and condition returns true, it executes the code inside if block. After this, it executes the next line outside if condition also.

public class IfDemo {

  public static void main(String[] args) {
    int a = 10;
    int b = 20;
    if(a<b) {
      System.out.println("a is lesser than b");
    }
    System.out.println("This is an example of If Statement");
  }

}
a is lesser than b
This is an example of If Statement

Now let’s consider the below example where the condition returns false. Hence it ignores the code block inside if condition and it executes the statement outside if block.

public class IfDemo {

  public static void main(String[] args) {
    int a = 10;
    int b = 20;
    if(a>b) {
      System.out.println("a is lesser than b");
    }
    System.out.println("This is an example of If Statement");
  }

}
This is an example of If Statement

Java If else Statement

If else in Java contain 2 code blocks which means when the condition or expression is true, it executes the code inside if block. When the expression is false, it executes the code inside else block. We use this conditional statement in Java when we want to evaluate both true and false status of an expression. This is the most common use of the decision-making statement in Java.

Syntax

if(condition) {
   //executes this code when condition is true
}
else {
   //executes this code when condition is false
}

//When there is only 1 statement inside if and else code block
if(condition)
   //execute this line when condition is true
else
   //execute this line when condition is false

Flowchart

If Else in Java

Example of If else Statement

In the below example, we have 2 variables x and y with values 50 and 20. Here the expression or condition is to check which value is greater. Since variable x value is greater than y, hence it prints the line inside if condition. Suppose the condition was false, then it executes the line inside else part.

public class IfElseDemo {

  public static void main(String[] args) {
    int x = 50;
    int y = 20;
    if(x>y)
      System.out.println("x is greater than y");
    else
      System.out.println("y is greater than x");
    

  }

}
a is greater than b

Java Nested If Statement

When there are multiple if condition statements within another if statement, we call it as a Nested if. The inner if condition executes only if the previous if condition expression is true. This is another type of Java conditional statement that contains multiple If else in Java.

Syntax

if(condition1) {
  //executes code when condition1 is true
  if(condition2) {
    //executes code when condition2 is true
  }
}

 

Flowchart

If Else in Java

Example of Nested If Statement

In this example, we are going to find which is the greatest number. Here we have 3 variables a, b and c with values 20, 10, and 30. The first if condition checks if a is greater than b. Since this is true(20>10), it executes the 2nd if condition inside. Here, it checks if a is greater than c. Since this condition is false, it executes the else part and assigns the value of c to variable num. Since the 1st if condition is executed, it ignores the else part and directly prints the output. In this example, we have 2 different if else conditions.

public class NestedIfDemo {

  public static void main(String[] args) {
    int a = 20;
    int b = 10;
    int c = 30;
    int num;
    
    if(a>b) {
      if(a>c) {
        num = a;
      }
      else
        num = c;
    }
    else {
      if(b>c) {
        num = b;
      }
      else
        num = c;
    }
    System.out.println("The greatest number is " + num);

  }

}
The greatest number is 30

Java If-Else if Statement

Suppose we have multiple if-else in Java, then we use the decision making statement if-else if ladder. This means when the 1st if condition is false, it checks the next else if condition and so on until the condition is true, otherwise, it executes the final else statement.

The else if statement is case sensitive and there should be a space between both the keywords. If we use elseif, it throws an error. This is another most commonly used if else statement in Java.

Syntax

if(condition1) {
  //execute code when condition1 is true
}
else if(condition2) {
  //execute code when condition2 is true
}
else if(condition3){
  //execute code when condition3 is true
}
else
{
  //execute code when none of the above condition is true
}

Flowchart

Java Decision Making Statements

Example of If-Else if Statement

In the below example, we assign a value “Java” to the variable lang. It first checks whether the variable lang is equal to “PHP” since it is false, it executes the next else if condition which checks if lang equals to “C”. Since this is also false, it executes the next else if condition and checks if it is equal to Java. Since this condition is true, it executes the line inside this if condition and ignores the final else part.

public class IfElseLadderDemo {

  public static void main(String[] args) {
    String lang = "Java";
    if(lang == "PHP") {
      System.out.println("Languae is PHP");
    }
    else if(lang == "C")
      System.out.println("Language is C");
    else if(lang == "Java")
      System.out.println("Language is Java");
    else
      System.out.println("No languages match");

  }

}
Language is Java

Conclusion

In this tutorial, we have learned various types of if else in Java along with different examples.

Reference

Translate »