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

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet