Lesson 7 of 34

else if: Many Choices

8 min 25 XP

Use else if when there are more than two choices. Java checks from top to bottom and runs only the first block whose condition is true.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Order matters! Check the biggest range first. If temp >= 15 came first, even 40 would print "Pleasant".

Challenge

+25 XP

Read a mark and print its grade: 90+ → A, 75–89 → B, 50–74 → C, below 50 → D

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.