/* */ Click to Join Live Class with Shankar sir Call 9798158723

Java if-else-if ladder


Java if-else-if ladder is used to work on multiple conditions.Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.The else if condition is checked only if all the conditions before it (in previous else if constructs, and the parent if constructs) have been tested to false.


Syntex :-

if (Condition1)
   "{"
        Statement1;
   "}"
else if(Condition2)
   "{"
        Statement2;
   "}"
   .
   .
   .
else if(ConditionN)
   "{"
        StatementN;
   "}"
else
   "{"
        Default_Statement;
   "}"

Q.) W.A.P to print the greater number using simple if-else-if ladder statement. ?
 
    public class else_if {
    public static void main(String args[]) {
        float avg;
        System.out.println("Enter The Average Mark : ");
        Scanner in = new Scanner(System.in);
        avg = in.nextFloat();
        if (avg >= 90 && avg <= 100) {
            System.out.println("Grade A");
        } else if (avg >= 80 && avg <= 89) {
            System.out.println("Grade B");
        } else if (avg >= 70 && avg <= 79) {
            System.out.println("Grade C");
        } else {
            System.out.println("Grade D");
        }
    }
}
Output
Enter The Average Mark :
84
Grade B