August 26, 2022

Java if-else Statement With Examples

In Java programming language there are two selection statements if-else and switch statement to control the execution flow. This post talks about if and if-else statements in detail with examples.

Java if-else statement

Java if-else statement is used to evaluate a condition and take the execution path based on whether the evaluated condition is true or false. Java if statement is very flexible and provides many combinations.

  • if statement
  • if-else statement
  • if-else-if statement
  • nested if statement

Java if statement

You can have just the if statement (else is optional).

Syntax of the Java if statement-

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

Here condition is a boolean expression. If condition is evaluated to true if block is executed, if condition evaluates to false then the if block is not executed. If there is only a single statement with in the if condition then the curly braces are optional.

Java if statement examples

public class IfDemo {
  public static void main(String[] args) {
    int age = 25;
    // check for age
    if(age > 18){
      System.out.println("Eligible to vote (age > 18)");
    }
    System.out.println("After if statement");
  }
}
Output
Eligible to vote (age > 18)
After if statement

Here the condition (age > 18) is evaluated to true so the if block is executed.

In the same program if age is passed as less than 18.

public class IfDemo {
  public static void main(String[] args) {
    int age = 15;
    // check for age
    if(age > 18)
      System.out.println("Eligible to vote (age > 18)");
    System.out.println("After if statement");
  }
}
Output
After if statement

Here the condition (age > 18) is evaluated to false so the if block is not executed. Note that the curly braces are not used with the if statement as there is only a single statement.

Java if-else statement

In Java if-else statement condition is evaluated and the execution flow is routed based on whether the condition is true or false.

Syntax of the Java if-else statement-

if(condition){
  //statement(s)
}else{
  //statement(s)
}

In the if-else statement if block is executed when condition is evaluated to true, if condition evaluates to false then the else block is executed.

if-else Java

Java if-else statement examples

public class IfDemo {
  public static void main(String[] args) {
    int age = 25;
    // check for age
    if(age > 18){
      System.out.println("Eligible to vote (age > 18)");
    }else{
      System.out.println("Not eligible to vote (age < 18)");
    }
    System.out.println("After if statement");
  }
}
Output
Eligible to vote (age > 18)
After if-else statement

Since condition evaluates to true so the if block is executed.

public class IfDemo {
  public static void main(String[] args) {
    int age = 15;
    // check for age
    if(age > 18){
      System.out.println("Eligible to vote (age > 18)");
    }else{
      System.out.println("Not eligible to vote (age < 18)");
    }
    System.out.println("After if-else statement");
  }
}
Output
Not eligible to vote (age < 18)
After if-else statement

Java if-else-if ladder

You can also have a series of if-else if statements where each if and else if statement has a condition and a particular block is executed if the condition associated with that block evaluates to true.

Java if-else-if syntax-
if(condition1){
  statement(s);
}else if(condition2){
  statement(s);
}else if(condition3){
  statement(s);
}
.
.
.
else{
  statement(s);
}

In if-else-if statement condition is evaluated from the top. Whichever condition evaluates to true statements associated with that block are executed and rest of the statements are bypassed. Last else statement acts as a default which is executed if none of the condition evaluates to true. But the lase else statement is optional if it is not present and all the conditions are false then no statement is executed and the control comes out of if-else-if.

Java if-else-if example

public class IfDemo {
  public static void main(String[] args) {
    int angle = 55;
    
    if(angle < 90){
      System.out.println("Acute angle");
    }else if (angle == 90){
      System.out.println("Right Angle");
    }else if (angle > 90 && angle < 180){
      System.out.println("Obtuse angle");
    }else if (angle == 180){
      System.out.println("Straight angle");
    }else {
      System.out.println("angle more than 180");
    }
  }
}
Output
Acute angle

In the code first if condition itself is evaluated to true so none of the other conditions are evaluated.

Java nested if-else

You can have a if-else statement inside a if-else statement in Java. It is known as a nested if-else statement.

Java nested if-else example

public class IfDemo {

  public static void main(String[] args) {
    int angle = 155;
    
    if(angle < 180){
      if(angle < 90)
        System.out.println("Acute angle");
      else if(angle > 90){
        System.out.println("Obtuse angle");
      }else{
        System.out.println("Right Angle");
      }
    }else {
      if (angle == 180){
        System.out.println("Straight angle");
      }else {
        System.out.println("angle more than 180");
      }
    }
  }
}
Output
Obtuse angle

That's all for the topic Java if-else Statement With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment