Java If-else

    Java supports testing the given condition in order to execute some statements. The if-condition will work on the boolean condition, either true or false. You will encounter a number of if-condition statements -

    • If
    • If-else
    • If-else-if
    • Nested if

    If statement

    This statement will allow you to test a condition that is specified by the user. This condition is followed by a set of instructions that will get executed only if the condition holds true.

    Syntax

    if(conditon){
    //code
    }

    Example

    public class demo_if {  
    public static void main(String[] args) {  
           int var1=20;  
        if(var1>18){  
            System.out.print("variable is more than 18");  
        }  
    }  
    }  

    The code will only get executed if the var1 is more than 18. Else, no output will be displayed.

    If-else statement

    This statement will allow you to test a condition that is specified by the user. This condition is followed by a set of instructions that will get executed only if the condition holds true else the else-code block will get executed.

    Syntax

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

    Example 1

    public class demo_IfElse {  
    public static void main(String[] args) {   
        int var=15;  
        //Check if the number is divisible by 2  
        if(var%2==0){  
            System.out.println("even number");  
        }else{  
            System.out.println("odd number");  
        }  
    }  
    }  

    In the above code, the ‘even number will be displayed only if the condition holds true else, the ‘odd number ‘ will be displayed. Alternative code for checking if the number is divisible by 2 or not.

    Example 2

    public class IfElseTernaryExample {    
    public static void main(String[] args) {    
        int var=13;    
        //Using ternary operator  
        String output=(var%2==0)?"even number":"odd number";    
        System.out.println(output);  
    }    
    }    

    If-else-if statement

    In this, a number of conditions will be checked. If the first condition is false, the else-f condition will be checked. If that fails too, the next else-if condition will be checked. If that fails, too, the last else statement will get executed.

    Syntax

    if(condition1){  
    //code 
    }else if(condition2){  
    //code to be executed 
    }else if(condition3){  
    //code to be executed
    }  
    ...  
    else{  
    //code to be executed if all the conditions fails to satisfy  
    }  

    Example

    public class demo_IfElseIf {  
    public static void main(String[] args) {  
        int marks=65;        
        if(marks<50){  
            System.out.println("fail");  
        }  
        else if(marks>=50 && marks<70){  
            System.out.println("D grade");  
        }  
        else if(marks>=70 && marks<90){  
            System.out.println("C grade");  
        }else if(marks>=90 && marks<100){  
            System.out.println("A+ grade");  
        }else{  
            System.out.println("Flase!");  
        }  
    }  
    }  

    Nested if statement

    You can declare an if statement within another if statement. If the outer if-condition is true only, the inner if-condition will be checked.

    Syntax

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

    Example

    public class demo_NestedIf {     
         Public static void main(String[] args) { 
            int age=20;       
            int weight=80;        
            if(age>=18){           
               if(weight>50){               
                 System.out.println("You are ready");                }        
             }     
          }
    }  

    Conclusion

    The Java if-else statement allows us to execute a block of code if a particular condition becomes true or false. The if-else statement generally checks whether a given condition is true or false. If the specified condition is true, it executes the statements in the if block. Otherwise, it executes the statements in the else block. This tutorial explains different types of if-else statements in Java and their examples. If you have any queries, feel free to post them in the comments section below.

    People are also reading: