Tuesday, 22 November 2016

If-Else ladder

If Else ladder is the sequence of if..else statements. It has following syntax:
if(condition)
   statement;
else if(condition)
   statement;
else if(condition)
   statement;
.
.
.
else
   statement;


The if statements are executed from the top down. If the condition is true, the statement associated with that if is executed, and the rest of the ladder is skipped. If none of the conditions is true, then the final else statement will be executed.
In this program, we will check the marks of student to find the grade using if-else ladder like Distinction, First class, Second class, Pass class and Fail as given in code.

PROGRAM
public class IfElseLadderDemo {

 public static void main(String[] args) {
  
  float marks = 62.12f;
  System.out.println("Marks : "+marks);
  System.out.print("Your grade is : ");
  
  if(marks >= 75)
   System.out.print("Distinction");
  else if(marks >= 60)
   System.out.print("First class");
  else if(marks >= 50)
   System.out.print("Second class");
  else if(marks >= 40)
   System.out.print("Pass class");
  else
   System.out.print("Fail");
 }
} 
OUTPUT
C:\>javac IfElseLadderDemo.java
C:\>java IfElseLadderDemo
Marks : 62.12
Your grade is : First class

No comments:

Post a Comment