Switch Case Statement with an Example in Java – Java Tutorial
Problem Statement
Explain the switch case statement with a programming example.
Solution:
The switch case statement is one of the very important control statements in the java programming language.
You can compare the switch case statement with a Menu-Card in the hotel. You have to select the menu then only the order will be served to you.
Here is a sample program that makes use of a switch case statement –
class switchcasedemo { public static void main(String args[]) throws java.io.IOException { char choice; System.out.printIn(" \tProgram for switch case demo'); System.out.println("Main Menu"); System.out.println("1. A"); System.out.println("2. B'); System.out.println("3. C'); System.out.println("4. None of the above"); System.out.println("Enter your choice'): Choice = (char)System.in.read(); switch(choice) { case '1':System.out.println('You have selected A'); break; case '2':System.out.println('You have selected B'); break; case '3':System.out.println('You have selected C'); break; default:System.out.println("None of the above choices made'); } } }
The output of the Switch Case Statement example:
Program for switch case demo
Main Menu
1. A
2. B
3. C
4. None of the above
Enter your choice 2
You have selected B
Summary:
In this article, we understood the switch case example in Java – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.