Java ProgramsControl FlowGrade Description Using Switch

Grade Description Using Switch in Java

beginner·  Control Flow  ·  Switch Statement

Problem

A switch statement can map a fixed set of letter grades directly to their descriptions, without any threshold comparisons.

Given a letter grade, print a short description of what it means.

Input
grade = 'B'
Output
Good

Java Program

Java
public class GradeDescriptionSwitch { public static void main(String[] args) { char grade = 'B'; String description; switch (grade) { case 'A': description = "Excellent"; break; case 'B': description = "Good"; break; case 'C': description = "Average"; break; case 'D': description = "Below Average"; break; case 'F': description = "Fail"; break; default: description = "Invalid grade"; // required so description is always assigned } System.out.println(description); } }

Output

Good

Core Logic

Matching the letter grade against each case directly looks up its description, without needing any range comparisons.

How It Works
  1. 1switch (grade) matches the character against each case label.
  2. 2'A' resolves to "Excellent", 'B' to "Good", 'C' to "Average", 'D' to "Below Average", and 'F' to "Fail".
  3. 3The default case handles any character that isn't one of the five recognized grades, printing "Invalid grade".
For grade = 'B', the case 'B' branch matches directly and prints "Good".
💡

Key Point: Unlike converting numeric marks into a grade, this assumes the grade letter is already known and just needs a description — a direct switch lookup replaces range comparisons entirely.

Key Concepts

switch statementchar switchdefault case

Related Programs