Java If-else

By | September 29, 2021

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 instruction which 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.

Vamware

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 instruction which will get executed only if the condition holds true else the else-code block will get executed.

Syntax

Vamware
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 condition will be checked. If the first condition is false then the else-f condition will be checked, if that fails too then the next else-if condition will be checked if that fails too then 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 then 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");                }        
         }     
      }
}  

People are also reading:

 

Leave a Reply

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