📖 Scenario: You are creating a simple program that assigns a grade based on a student's score.
🎯 Goal: Build a Java program that uses an else-if ladder to print the correct grade for a given score.
📋 What You'll Learn
Create an integer variable called score with the exact value 85.
Create an integer variable called passingScore with the exact value 50.
Use an else-if ladder with the variable score to assign grades:
90 and above is Grade A
80 to 89 is Grade B
70 to 79 is Grade C
60 to 69 is Grade D
50 to 59 is Grade E
Below 50 is Fail.
Print the grade using System.out.println.
💡 Why This Matters
🌍 Real World
Grading systems in schools or colleges use similar logic to assign letter grades based on numeric scores.
💼 Career
Understanding conditional statements like else-if ladders is essential for decision-making in software development.
Progress0 / 4 steps
1
Create the score variable
Create an integer variable called score and set it to 85.
Java
Hint
Use int score = 85; inside the main method.
2
Create the passingScore variable
Add an integer variable called passingScore and set it to 50 below the score variable.
Java
Hint
Declare int passingScore = 50; just after score.
3
Write the else-if ladder for grades
Write an else-if ladder using the variable score to assign grades as follows: if score >= 90, print Grade A; else if score >= 80, print Grade B; else if score >= 70, print Grade C; else if score >= 60, print Grade D; else if score >= 50, print Grade E; else print Fail. Use System.out.println inside each block.
Java
Hint
Use if, multiple else if, and a final else to cover all score ranges.
4
Print the grade result
Run the program to print the grade for the score 85. The output should be Grade B.
Java
Hint
Run the program and check the console output.
Practice
(1/5)
1. What does an else-if ladder in Java help you do?
easy
A. Run all conditions regardless of their truth
B. Check multiple conditions one by one until one is true
C. Only check the first condition and ignore others
D. Skip conditions and run else block always
Solution
Step 1: Understand the purpose of else-if ladder
An else-if ladder lets you test several conditions in order, stopping at the first true one.
Step 2: Compare options with this behavior
Only Check multiple conditions one by one until one is true correctly describes checking conditions one by one until one is true.
Final Answer:
Check multiple conditions one by one until one is true -> Option B
Quick Check:
Else-if ladder = check conditions one by one [OK]
Hint: Else-if ladder stops at first true condition [OK]
Common Mistakes:
Thinking all conditions run regardless
Confusing else-if with separate if statements
Believing else runs before conditions
2. Which of the following is the correct syntax for an else-if ladder in Java?
easy
A. if(condition) { } else if condition { } else { }
B. if(condition) { } else(condition) { } else { }
C. if condition { } else if(condition) { } else { }
score >= 80? No (75 < 80). Next condition. score >= 70? Yes (75 >= 70). So print "Grade C" and skip else.
Final Answer:
Grade C -> Option A
Quick Check:
75 is between 70 and 79 = Grade C [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
Choosing Grade B or A by mistake
Ignoring else-if order
Assuming else runs even if condition true
4. Find the error in this else-if ladder code snippet:
int num = 10;
if(num > 10) {
System.out.println("Greater than 10");
} else if num == 10 {
System.out.println("Equal to 10");
} else {
System.out.println("Less than 10");
}
medium
A. Missing semicolon after print statements
B. Using else instead of else-if
C. Missing parentheses around condition in else-if
D. No error, code is correct
Solution
Step 1: Check syntax of else-if condition
In Java, conditions must be inside parentheses. Here, else if num == 10 lacks parentheses.
Step 2: Verify other parts
Print statements have semicolons, else-if is used correctly, so no other errors.
Final Answer:
Missing parentheses around condition in else-if -> Option C
Quick Check:
Conditions need parentheses [OK]
Hint: Always put conditions inside parentheses [OK]
Common Mistakes:
Forgetting parentheses in else-if
Confusing else and else-if syntax
Missing semicolons (not the issue here)
5. You want to write a program that assigns a letter grade based on score: - 90 and above: A - 80 to 89: B - 70 to 79: C - 60 to 69: D - Below 60: F Which else-if ladder correctly implements this?