Decision making in Java structures have one or more conditions to be evaluate or test by the program, along with a statement or statements that are to be execute if the condition is determine to be true, and optionally, other statements to be execute if the condition is determine to be false.

There are six Selection statements in Java.

  • If statement
  • If-else statement
  • If-else-if ladder
  • Nested if statement
  • Switch statement
  • Jump statement

If Statement

The Java if statement tests the condition. It executes the if block if condition is true.

Flow Chart

Syntax

if(condition) 
{
   // Statements to execute if
   // condition is true
}
If Statement in Java

Example of If Statement

Input Value

public class ifv {
     
        public static void main(String[] args) {    
            int age=56;    
            if(age>25){  
                System.out.print("Age is greater than 25");  
            }  
        }  
        
}
If Statement in Java

Let’s run the code

Input Values

Output Value

Output Values

If-else Statement

The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executes.

Flowchart

Syntax

if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}
If-else Statement in Java

Example of If Statement

Input Value

public class elsev {
     
        public static void main(String[] args) {  
              
            int number=60;  
             
            if(number%2==0){  
                System.out.println("even number");  
            }else{  
                System.out.println("odd number");  
            }  
        }  
        
}
If-else Statement in Java

Let’s run the code

Input Values

Output Value

Output Value

If-else-if ladder

The if-else-if ladder statement executes one condition from multiple statements.

Flowchart

Syntax

if (condition)
    statement;
else if (condition)
    statement;
.
.
else
    statement;
If-else-if ladder

Example of If Statement

Input Value

public class elseifv {
      
        public static void main(String[] args) {  
            int marks=85;  
              
            if(marks<40){  
                System.out.println("fail");  
            }  
            else if(marks>=45 && marks<60){  
                System.out.println("D grade");  
            }  
            else if(marks>=62 && marks<72){  
                System.out.println("C grade");  
            }  
            else if(marks>=72 && marks<82){  
                System.out.println("B grade");  
            }  
            else if(marks>=82 && marks<90){  
                System.out.println("A grade");  
            }else if(marks>=90 && marks<100){  
                System.out.println("A+ grade");  
            }else{  
                System.out.println("Invalid!");  
            }  
        }  
        
}
If-else-if ladders

Let’s run the code

Input Values

Output Value

Output Value

Nested if Statement

The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.

Flowchart

Syntax

if (condition1) 
{
   // Executes when condition1 is true
   if (condition2) 
   {
      // Executes when condition2 is true
   }
}
Nested if Statement in Java

Example of If Statement

Input Value

public class neastedv {
       
        public static void main(String[] args) {    
             
            int age=30;  
            int weight=80;    
            
            if(age>=25){    
                if(weight>50){  
                    System.out.println("You are eligible to donate blood");  
                }    
            }    
        } 
}
Nested if Statement in Java

Let’s run the code

Input Values

Output Value

Output Value

Switch Statement

Syntax

switch (expression)
{
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;
  .
  .
  case valueN:
    statementN;
    break;
  default:
    statementDefault;
}
Syntax of Switch Statement

Example of If Statement

Input Value

public class arrays {

	public static void main(String[] args)
	{
		int day = 2;
		String dayString;

		switch (day) {

		case 1:
			dayString = "Monday";
			break;
		
		case 2:
			dayString = "Tuesday";
			break;

		case 3:
			dayString = "Wednesday";
			break;
			
		case 4:
			dayString = "Thursday";
			break;

		case 5:
			dayString = "Friday";
			break;

		case 6:
			dayString = "Saturday";
			break;
	
		case 7:
			dayString = "Sunday";
			break;


		default:
			dayString = "Invalid";
		}
		System.out.println(dayString);
	}
}
Switch Statement

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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