If else statement in Java

If-else Statement in Java

If-else statement in Java is used to evaluate conditions specified using relational or comparison operators. To include more than one condition together, logic operators are used. These conditions result in either of the boolean values which are true or false. Java provides variations to the if-else statements which are:

    • if statement in Java

    • if-else statement in Java

    • if-else-if ladder in Java

    • nested if statement in Java

 

 

Java If Statement

If statement executes a block of code when the condition specified in the if statement is true. Otherwise, the if-block is ignored like it was never in the program and execution continues normally after it.

Syntax:

if(condition){
//code to be executed
}

Flowchart:

if statement

 

Example of If statement in Java:

//Java program with if statement
public class IfStatement {
    public static void main(String[] args){
        //Defining a variable "age" of type int initialized to 18
        int age=18;

        //if condition to check if age is greater than or equal to 18
        if(age>=18){
            System.out.println("You are eligible to vote in India!!");
        }
    }
}

In the program provided above, a variable of type int has been created on which condition has been applied to check if the variable “age” has a value greater than or equal to 18. If it has, a string is printed on the console stating “You are eligible to vote in India!!”.

Output:

You are eligible to vote in India!!

 

Java If-else Statement

If-else statement consists of two blocks, if-block, and else-block. If-block is executed when the if the condition is true and else-block is executed when the if-condition is false. But these two blocks are not executed one after the other. Only one will execute depending on the condition. If the if-block executes, then the else-block will not and vice-versa.

Syntax:

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

 

Flowchart:

if else statement

 

Example of If-else statement in Java:

//Java program with if-else statement
public class IfElseStatement {
    public static void main(String[] args){
        //defining an integer type variable with value 23
        int number=23;

        //if condition to check if variable "number" is even
        if(number%2==0){
            //Executes when if-condition is true
            System.out.println("Number "+number+" is even.");
        }else{
            //executes when if condition is false
            System.out.println("Number "+number+" is odd.");
        }
    }
}

In the program provided above, a variable of type int has been created and initialized to 23. It consists of an if-else statement where if-condition checks if a number of divisible by 2 using modulus operator(%). If it is divisible by 2, if-block will execute, otherwise else-block will execute stating that the variable “number” is an odd number.

Output:

Number 23 is odd.

 

 

Java If-else-if Statement

If-else-if statement consists of as many blocks as you need depending upon your program. It is used when you need to specify more than one condition. The first condition is checked is if-condition. If evaluated and results in true, then its code block is executed, lest next else-if condition is checked and if evaluated to true, then its code block is executed. This procedure goes on until it reaches the last else-block. It is reached only when all the prior conditions were evaluated to false.

Syntax:

if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
...
else if(conditionN){
//code to be executed if conditionN is true
}else{
//code to be executed if none of the conditions mentioned above evaluates to true
}

 

Flowchart:

if else if statement

 

Example of If-else-if statement in Java:

//Java program with if-elseif statement
public class IfElseIfStatement {
    public static void main(String args[]){
        //defining a variable "number" with value -10
        int number=-10;
        if(number>0){
            //executes when if-condition is true
            System.out.println("Number "+number+" is positive.");
        }else if(number<0){
            //executes when else-if condition is true
            System.out.println("Number "+number+" is negative.");
        }else{
            //executes when neither of the conditions mentioned in if and else-if are true
            System.out.println("Number "+number+" is neither positive nor negative.");
        }
    }
}

In the program provided above, a variable “number” has been created with a value of -10. It is then compared with 0 to see if it is greater than it implies that it is a positive number. If this condition results in false, then else if the condition is checked to see if it is less than it implies that it is a negative number. It also results in true, it means that the “number” is neither negative nor positive which means it is 0.

Output:

Number -10 is negative.

 

 

Java Nested If Statement

Nested if statement allows one or more if statement variations in it. Inner if statements will execute or run only if the immediate outer if statement is true.

Syntax:

if(condition){
//code to be executed
  if(condition){
  //code to be execeuted
  }
}else{
//code to be executed
}

 

Flowchart:

Nested if statements

 

Example nested if statement in Java:

//Java program with nested if statement
public class NestedIf {
    public static void main(String[] args){
        //defining a variable "number" with value 30
        int number =30;
        if(number%3==0){
            //executes if number is divisible by 3
            if(number%5==0){
                //executes if number is divisible both by 3 and 5
                System.out.println("Number "+number+" is divisible by 15.");
            }
        }else{
            //executes if number is not divisible by 3
            System.out.println("Number "+number+" is not divisible by 15.");
        }
    }
}

In the program provided above, a variable named “number” has been created and initialized to 30. It is then compared with 3 to check if it is divisible by it using the modulus operator(%). If it is divisible by 3, then the number is compared with 5 to see if it is divisible by it. If it is, then it means it is divisible both by 3 and 5, making it a multiple of 15. If the “number” is not divisible by 3 then it is not divisible by 15 which is printed to the console when else block is executed.

Output:

Number 30 is divisible by 15.

 

Note:

If if-block, else-block, or else-if-block needs only one statement, then it can be written without using the curly braces.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *