if …else statements

  • Last Updated: October 7, 2023
  • By: javahandson
  • Series
img

if …else statements

In this article, we will learn what is if …else statements and different types of if …else statements in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.

 

if statement

The if statement says if the given condition is true then execute a particular set of statements.

if (condition) {
   // Code to be executed if the condition is true
}

We can write multiple statements in the if block. If we have to specify multiple statements in the if block then we have to use curly braces. But if we have only one statement in the if block then curly braces are not needed i.e. optional.

Write a program to check if the number is even.

package com.java.handson.control.statements;

public class Test {
    public static void main(String[] args) {

        int number = 8;

        if(number % 2 == 0) {
            System.out. println(number + " is an even number");
            System.out. println(number + " is divisible by 2");
        }

        number = 10;

        // No curly braces used below as only one statement in if block        
        if(number % 2 == 0)
            System.out. println(number + " is an even number");
    }
}
Output: 8 is an even number
8 is divisible by 2
10 is an even number

if-else statement

The if-else control statement says if the given condition is true then execute a particular set of statements else execute another set of statements.

if (condition) {
   // Code to be executed if the condition is true
}
else {
  // Code to be executed if the condition is false
}

We can write multiple statements in the if and else block. If we have to specify multiple statements in either of the blocks then we have to use curly braces. But if we have only one statement in either of the blocks then curly braces are not needed i.e. optional.

Write a program to check if the number is even or odd.

package com.java.handson.control.statements;

public class Test {
    public static void main(String[] args) {

        int number = 8;

        if(number % 2 == 0) {
            System.out. println(number + " is an even number");
            System.out. println(number + " is divisible by 2");
        }
        else {
            System.out. println(number + " is an odd number");
        }

        number = 7;

        // No curly braces used below as only one statement in if and else block 
        if(number % 2 == 0)
            System.out. println(number + " is an even number");
        else
            System.out. println(number + " is an odd number");

    }
}

Output : 8 is an even number
8 is divisible by 2
7 is an odd number

if-elseif statement

In the above statements we have specified only one condition but suppose we have to specify multiple conditional blocks then we can achieve that by using the if-elseif control statement.

if (condition1) {
   // Code to be executed if the condition 1 is true
}
else if (condition2) {
  // Code to be executed if the condition 2 is true
}
else if (condition3) {
  // Code to be executed if the condition 3 is true
}
else {
  // Code to be executed if no condition matches
}

Note* Only one block will be executed and the first matching block will be executed.

Write a program to check if the number is divisible by 5.

package com.java.handson.control.statements;

public class Test {
    public static void main(String[] args) {

        int number = 10;

        if(number % 3 == 0) {
            // block-1
            System.out. println(number + " is  divisible by 3");
        }
        else if (number % 5 == 0) {
            // block-2
            System.out. println(number + " is divisible by 5");
        }
        else if (number % 10 == 0) {
            // block-3
            System.out. println(number + " is divisible by 10");
        }
        else {
            // block-4
            System.out. println(number + " is not divisible by 3, 5 and 10");
        }
    }
}

Output : 10 is divisible by 5

In the above example, number 10 is divisible by both 5 and 10 but only the block-2 got executed because it comes before block-3.

If neither of the condition matches then the else block gets executed.

Nested If

An if block inside another if block is known as nested if.

If first if condition matches then we check the nested if condition. If the nested if condition also matches then the set of statements gets executed.

if (condition1) {
   if (condition2) {  
      // Code to be executed if both condition 1 and condition 2 matches
   }  
}

We can have multiple nested if conditions as well.

if (condition1) {
   if (condition2) {
        if (condition3) {
         // Code to be executed if condition 1 , condition 2 and condition 3 matches
     }  
  }  
}

Write a program to check if the number is divisible by both 2 and 5.

package com.java.handson.control.statements;

public class Test {
    public static void main(String[] args) {

        int number = 20;

        if (number % 2 == 0) {
            if (number % 5 == 0) {
                System.out.println(number + " is divisible by 2 and 5");
            }
        }
    }
}
Output : 20 is divisible by 2 and 5

So this is all about if …else statements in Java. I hope you like the article. If you have any questions on this topic please raise them in the comments section.

Leave a Comment