- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java switch statement example
Following is a running program of the switch statement.
Example
public class Test { public static void main(String args[]) { // char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }
Output
Compile and run the above program using various command line arguments. This will produce the following result −
Well done Your grade is C
- Related Articles
- Java switch statement
- Switch Statement in Java
- Java break statement with switch
- Java switch statement with multiple cases.
- Java if statement example
- PHP switch Statement
- Java Program to Implement switch statement on strings
- Java nested if statement example
- Can we use Switch statement with Strings in java?
- Kotlin 'when' statement vs Java 'switch'
- Switch case statement in C
- Can you use a switch statement around an enum in Java?
- How to define a switch statement in JShell in Java 9?
- Explain switch statement in C language
- What is Switch...case statement in JavaScript?

Advertisements