Java If Else Control Statement

  In this section, we will learn about Control Statements, you will learn Java If Else, nested-if, if control statement in a brief.

Introduction to If Else Java

In Java, if-else is a control statement that allows users to control the instruction for particular conditions. Control statements are instructions having a condition, if the conditions meet the requirements then it will execute the given instructions.

  1. If statement.
  2. If else statement.
  3. If else if statement(also known as ladder).
  4. Nested if else statement.

Table of Contents

Java If Statement

Java If statement is a condition block statement, if the condition is true then it will execute the if block statement, Java If Control Statement Syntax Shown Below:

   
 
    
if(condition){
		/*** 
		* statements
		*/
}

    

Flow Chart of Java If Statement

Flow Chart – Java If Statement

Example: Java If Statement

   
 
    
public class IfExample{

     public static void main(String []args){
        int yourage = 15;
        
        if(yourage<18){
             System.out.println("Sorry! You are not eligible for vaccination...");
            
        }
        
     }
}
    

Output:

Sorry! You are not eligible for vaccination...

Java If-Else Statement

Java if-else statement is also a condition block statement, if the condition is true then it will execute the if block statement otherwise else blocks statements.
Java If-Else Control Statement Syntax Shown Below:

   
 
    
if(condition){
		/*** 
		* statements
		*/
else if(condition2){
		/*** 
		* statements2
		*/
}
    

Flow Chart of If-Else Statement

Flow Chart – Java If Else Statement

Example: Java If-Else Statement

   
 
    
public class IfElseExample{

     public static void main(String []args){
        int yourage = 24;
        
        if(yourage<18){
             System.out.println("Sorry! You are not eligible for vaccination.");
            
        }
        else{
             System.out.println("Good ! You are eligible for vaccination.");
            
        }
        
     }
}
    

Output:

Good ! You are eligible for vaccination.

Java If-Else-If Statement

Java if-else-if statement checked the multiple conditions, if the condition meets the requirements then it will execute the respective block statement.
Java if-else-if Control Statement Syntax Shown Below:

   
 
    
if(condition){
		/*** 
		* statements
		*/
else if(condition2){
		/*** 
		* statements2
		*/
}	
else if(condition3){
		/*** 
		* statements3
		*/
}
    

Flow Chart of If-Else-If Statement

Java if-else-if Statement

Java If-Else-If Statement Example

   
 
    
/***
 * A program to check the grade based on numbers
 */

public class IfElseIfExample{

     public static void main(String []args){
        // create score variable to check grade
        int score = 85;
        
        if(score>90){
            // print the message A+ grade if score greater the 90.
            System.out.println("Congratulations! You Got A+ Grade ");
            
        }
        else if(score>70 && score<90){
            // print the message B+ grade if score greater the 70 and less then 90.
             System.out.println("Congratulations! You Got B+ Grade ");
        }
        else if(score>60 && score<70){
             // print the message C+ grade if score greater the 60 and less then 70.
             System.out.println("Congratulations! You Got C+ Grade ");
        }
        else if(score>40 && score<60){
             // print the message D+ grade if score greater the 40 and less then 60.
             System.out.println("Congratulations! You Got D+ Grade ");
        }
        else {
            // print the message F Grade for fail, if the score is  less then 40.
             System.out.println("Oops! You Failed, Got F Grade.");
        }
        
     }
}
    

Output:

Congratulations! You Got B+ Grade.

Java Nested-If Statement

Java nested-if statement allows the user to control the flow of nested conditions. Nested if statement checks the outer condition first, if it is true then it will execute the inner if statement and check the conditions.
Java nested-if Control Statement Syntax Shown Below:

   
 
    
if(condition){
		/*** 
		* statements 1
		*/
		
		// inner if-else statments
		if(condition2){
			 // statements n
		}
}

    

Flow Chart of Nested-If Statement

Java Nested-If Statement.

Java Nested If Statement Example

   
 
    
public class NestedIfExample{

     public static void main(String []args){
        int yourage = 24;
        String health="Good";
        
        // outer - if-else statments
        if(yourage>18){
             // nested if-else statements
             if(health=="Good"){
             
              System.out.println("Good ! Your Health is Good. You are eligible for vaccination.");
               
            }
            else{
                System.out.println("Your Health is not well. You are not eligible for vaccination.");
               
            }
           
        }
        else{
             System.out.println("Sorry! You are not eligible for vaccination.");
            
        }
        
     }
}

    

Output:

Good ! Your Health is Good. You are eligible for vaccination.

Leave a Reply

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