0% found this document useful (0 votes)
3 views5 pages

3.if-Else Ladder

The document explains the Java if-else-if ladder statement, which allows for executing one condition from multiple statements based on given conditions. It provides syntax and examples, including a grading system and a program to check if a number is positive, negative, or zero. The examples demonstrate how the if-else-if structure works in practice, with corresponding outputs for different scenarios.

Uploaded by

mohol college
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

3.if-Else Ladder

The document explains the Java if-else-if ladder statement, which allows for executing one condition from multiple statements based on given conditions. It provides syntax and examples, including a grading system and a program to check if a number is positive, negative, or zero. The examples demonstrate how the if-else-if structure works in practice, with corresponding outputs for different scenarios.

Uploaded by

mohol college
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java if-else-if ladder Statement

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

Syntax:

1. if(condition1)
2. {
3. //code to be executed if condition1 is true
4. }
5. else if(condition2)
6. {
7. //code to be executed if condition2 is true
8. }
9. else if(condition3)
10. {
11. //code to be executed if condition3 is true
12. }
13. ...
14. lse
15. {
16. //code to be executed if all the conditions are false
17. }
Example:

1. //Java Program to demonstrate the use of If else-if ladder.


2. //It is a program of grading system for fail, D grade, C grade, B grade, A grade
and A+.
3. public class IfElseIfExample
4. {
5. public static void main(String[] args)
6. {
7. int marks=65;
8.
9. if(marks<50)
10. {
11. System.out.println("fail");
12. }
13. else if(marks>=50 && marks<60)
14. {
15. System.out.println("D grade");
16. }
17. else if(marks>=60 && marks<70)
18. {
19. System.out.println("C grade");
20. }
21. else if(marks>=70 && marks<80)
22. {
23. System.out.println("B grade");
24. }
25. else if(marks>=80 && marks<90)
26. {
27. System.out.println("A grade");
28. }
29. else if(marks>=90 && marks<100)
30. {
31. System.out.println("A+ grade");
32. }
33. else
34. {
35. System.out.println("Invalid!");
36. }
37. }
38. }

Output:

C grade
Program to check POSITIVE, NEGATIVE or ZERO:

1. public class PositiveNegativeExample


2. {
3. public static void main(String[] args)
4. {
5. int number=-13;
6. if(number>0)
7. {
8. ` `` System.out.println("POSITIVE");
9. }
10. else if(number<0)
11. {
12. System.out.println("NEGATIVE");
13. }
14. else
15. {
16. System.out.println("ZERO");
17. }
18. }
19. }

Output:

NEGATIVE

You might also like