Use a block body and yield to compute a switch branch value.
int score = 82;
String grade = switch (score / 10) {
case 10, 9 -> "A";
case 8 -> { yield score >= 85 ? "B+" : "B"; }
case 7 -> "C";
default -> "F";
};
System.out.println(grade);
B
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26