Java If-else Statement

The if statement in Java is utilized to evaluate a condition that is either true or false. There are several types of if statements that can be used in Java to create conditional statements with different outcomes based on the evaluated condition.

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

if Statement in Java:

In Java, the if statement is utilized to verify a specific condition. If the condition evaluates to true, the if block of code is executed.

Syntax:

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

Example:

//Java Example Program to demonstate the use of if statement.  
public class IfExample {  
public static void main(String[] args) {  
    //defining an 'age' variable  
    int age=20;  
    //checking the age  
    if(age>15){  
        System.out.print("Age is greater than 15");  
    }  
}  
}

Output:

Age is greater than 15

if-else Statement in Java:

In Java, the if-else statement is used to assess a condition. It executes the if the block of code if the condition is determined to be true. However, if the condition is found to be false, the else block of code is executed.

Syntax:

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

Example:

//A Java Program to demonstrate the use of if-else statement.  
//It is a program of odd and even number.  
public class IfElseExample {  
public static void main(String[] args) {  
    //defining a variable  
    int number=13;  
    //Check if the number is divisible by 2 or not  
    if(number%2==0){  
        System.out.println("even number");  
    }else{  
        System.out.println("odd number");  
    }  
}  
}

Output:

odd number

Leap Year Example:

public class LeapYearExample {    
public static void main(String[] args) {    
    int year=2020;    
    if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){  
        System.out.println("LEAP YEAR");  
    }  
    else{  
        System.out.println("COMMON YEAR");  
    }  
}    
}

Output:

LEAP YEAR

Β Ternary Operator:

In Java, the ternary operator (? πŸ™‚ offers a concise alternative to the if…else statement for condition checking. It provides a shorthand approach to evaluating a condition. When the condition is true, the value preceding the? the symbol is returned. Conversely, when the condition is false, the value following the: symbol is returned.

Example:

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

Output:

odd number

if-else-if ladder Statement in Java:

The if-else-if ladder statement in Java is used to evaluate multiple conditions and execute the corresponding block of code based on the first condition that evaluates to true.

Syntax:

if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}  
else if(condition3){  
//code to be executed if condition3 is true  
}  
...  
else{  
//code to be executed if all the conditions are false  
}

Example:

//Java Program to demonstrate the use of If else-if ladder.  
//It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.  
public class IfElseIfExample {  
public static void main(String[] args) {  
    int marks=65;  
      
    if(marks<50){  
        System.out.println("fail");  
    }  
    else if(marks>=50 && marks<60){  
        System.out.println("D grade");  
    }  
    else if(marks>=60 && marks<70){  
        System.out.println("C grade");  
    }  
    else if(marks>=70 && marks<80){  
        System.out.println("B grade");  
    }  
    else if(marks>=80 && marks<90){  
        System.out.println("A grade");  
    }else if(marks>=90 && marks<100){  
        System.out.println("A+ grade");  
    }else{  
        System.out.println("Invalid!");  
    }  
}  
}

Output:

C grade

Nested if Statement in Java:

In Java, the nested if statement involves placing an if block inside another if block. In this structure, the inner if block’s condition is evaluated and executed only when the outer if block’s condition is determined to be true.

Syntax:

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

Example:

//Java Program to demonstrate the use of Nested If Statement.  
public class JavaNestedIfExample {    
public static void main(String[] args) {    
    //Creating two variables for age and weight  
    int age=20;  
    int weight=80;    
    //applying condition on age and weight  
    if(age>=18){    
        if(weight>50){  
            System.out.println("You are eligible to donate blood");  
        }    
    }    
}}

Output:

You are eligible to donate blood

For authentic content and to explore more topics like Java If-else Statement, kindly stay connected with tutorials.freshersnow.com.