Decision Making in Java Programming Language

Rumman Ansari   Software Engineer   2022-09-08   7653 Share
☰ Table of Contents

Table of Content:


if else if ladder

In this section we will learn about Decision Making in java if-else-if ladder.

if else ladder in java programming language

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  
}  

Practical Approach of if else if Ladder

	//IfElseLadder.java

    class IfElseLadder
    {
        public static void main(String args[])
        {
             int x = 40;

			      if( x == 5 ) {
			         System.out.println("Value of X is 5");
			      }else if( x == 10 ) {
			         System.out.println("Value of X is 10");
			      }else if( x == 30 ) {
			         System.out.println("Value of X is 30");
			      }if( x == 35 ) {
			         System.out.println("Value of X is 35");
				  }else{
					  System.out.println("This is else statement");
				     }

        }
    }
Output
 
This is else statement
Press any key to continue . . .

Another example of if else if Ladder

import java.util.Scanner;
public class IfElseIfExample {
public static void main(String[] args) {
	int marks;
    System.out.println("Enter Marks");
    Scanner sc = new Scanner(System.in);

    marks=sc.nextInt();

    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
 
Enter Marks
75
B grade
Press any key to continue . . .