Switch Statement

Switch Statement

How we can use switch case in java program ?

View Answers

June 11, 2012 at 5:06 PM

Note:-Switch case provides built-in multiway decision statement.It switches to statement by testing the value.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class SwitchTest {
         public static void main(String [] args) throws IOException{
             int days= 0;
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter The Number");
            String data = br.readLine();
            days=  Integer.parseInt(data);

             switch(days){

             case 1: System.out.println("Sunday");
             break;
             case 2:  System.out.println("Monday");
             break;
             case 3:  System.out.println("Tuesday"); 
             break;
             case 4:  System.out.println("Wednesday"); 
             break;
             case 5:  System.out.println("Thursday");
             break;
             case 6:  System.out.println("Friday");
             break;
             case 7:  System.out.println("Saturday");
             break;
            default: System.out.println("Invalid days.");
             break;
         }
     }
}

Output

Enter the number 
3
Tuesday

Description: - In this example, we are using switch case statement. The program displays the name of the days according to user choice. As we inputted 3 so it switches to the case 3 and prints the value of case 3.









Related Tutorials/Questions & Answers:
switch statement
switch statement   i want to write a java program that computes Fibonacci,factorial,string reversal and ackerman using switch case to run as a single program
The Switch statement
. To avoid this we can use Switch statements in Java. The switch statement is used... of a variable or expression. The switch statement in Java is the best way to test.... Here is the general form of switch statement: switch (expression){ case 1
Advertisements
Switch Statement
switch case statement. The program displays the name of the days according to user...Switch Statement  How we can use switch case in java program ?   Note:-Switch case provides built-in multiway decision statement.It
Switch Statement with Strings in Java
Switch Statement with Strings in Java  Switch Statement with Strings in Java
Java switch statement
Java switch statement  What restrictions are placed on the values of each case of a switch statement
Switch statement in PHP
Switch statement in PHP  HII, Explain about switch statement in PHP?   hello,ADS_TO_REPLACE_1 Switch statement is executed line by line... of the switch expression. It does execute the statement until the end of the block
What is the difference between an if statement and a switch statement?
What is the difference between an if statement and a switch statement?   Hi, What is the difference between an if statement and a switch statement... values). Whereas the Switch statement offers you multiple choice of option from
Switch Statement example in Java
Switch Statement example in Java    ... for implementing the switch statement. Here, you will learn how to use the switch statement... for understanding more about the switch statement in Java. The following java
Java Switch Statement
Java Switch Statement       In java, switch is one of the control statement which turns the normal flow... then switch statement can be used. It checks your choice and jump on that case label
How to use switch statement in jsp code
How to use switch statement in jsp code       switch is a type of control statement used... for in the switch statement are met or executed.ADS_TO_REPLACE_1 Break
Switch Statement in java 7
Switch Statement in java 7 This tutorial describes the if statement in java 7. This is one kind of decision making statement. Switch Statements : The switch statement is used when you want to test many statements based on some
i need to replace this if statement code with switch case code
i need to replace this if statement code with switch case code   i need to replace this code with switch case for(int i=0;i<100;i++){ if((i...){ switch(i/j){ case 3: System.out.println ("java
Find Capital using Switch-Case statement
Find Capital using Switch-Case statement In this Java Tutorial section, we are going to find the capital of our states using Switch-Case statement. For this, we have created two arrays i.e. array of states and array of capitals. Then we
Strings in Switch statement
Strings in Switch statement In this section, you will learn about strings..., you can pass string as expression in switch statement. The switch statement... have matched string. The comparison of string in switch statement is case
C Break with Switch statement
C Break with Switch statement   ... the user to enter any number. The expression defined in the switch statement finds..., the switch statement checks the condition and shows the second case i.e odd If you
Switch case in java
if statementswitch allow the user to select a option from multiple options. switch statement have multiple number of possible execution path. It work... depending on the choice of user. The body of the switch statement is called switch
Switch
statements. To avoid this we can use Switch statements in Java. The switch statement... of a variable or expression. The switch statement in Java is the best way to test.... Here is the general form of switch statement: switch (expression){ case 1
Java - The switch construct in Java
;    Switch is the control statement in java which.... In this Program you will see that how to use the switch statement... flow of control quits from the Switch block whenever break statement occurs
Switch Case in Java
if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths... the default statement will be executed. A switch block is the body of a switch... case label A statement in the switch block can be labeled with one or more case
for statement
for statement  for(int i=0;i<5;i++); { system.out.println("The value of i is :"+i); } if i end for statement what will be the output   got the answer.. it displays only the last iteration that is "The value of i
PHP Switch Case
the statements until it found a break statement or the end of the switch case block...Switch Case Control Structure: Almost every modern language supports switch case control structure. It is similar to if statements. Switch case is useful
Break Statement in JSP
are using the switch statement under which we will use break statement. The for loop...Break Statement in JSP          The use of break statement is to escape
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
switch case
switch case  program to input 2 numbers and a choice.Using switch case we need to perform all the mathematical operation.Print a suitable error message if the choice is wrong
Switch databases
Switch databases  How do you inter-change between relational databases without code changes
switch Java Keyword
:  -- The switch statement in java language enables the user to select... statement at the end of each case block to exit from the switch statement. -- If the break statement is lacking in the switch case then the flow of execution will flow
programes on switch
=input.nextInt(); switch(choice){ case 1
switch case instead of if else
switch case instead of if else  How to write code in switch case instead of if else in Java
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
ModuleNotFoundError: No module named 'switch'
ModuleNotFoundError: No module named 'switch'  Hi, My Python... 'switch' How to remove the ModuleNotFoundError: No module named 'switch'... to install padas library. You can install switch python with following command
Statement block
Statement block  What is the purpose of a statement block
using switch,break and for loop
using switch,break and for loop  generate a 10 digit number and display the length of longest increasing series
using switch and break
using switch and break  generate a 10 digit number and display the length of longest increasing series
if statement in php
if statement in php  Correct way of writing if statement in PHP
java if statement
java if statement  If statement in Java
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
How to break statement in JSP
How to break statement in JSP  Hi, I am writing JSP page and there is for loop inside it. How to break statement in JSP? Thanks   Hi, I JSP you can use the break statement to break a loop. Here is simple example
Switch case + condition??
Switch case + condition??  I try to use a switch case with condition but it doesn't display anything have look: ......public void commandAction...(); } } else { List down = (List)display.getCurrent(); switch
PHP Conditional Statement
supports if , else, switch case and conditional operator for this purpose. PHP Conditional Statement Example 1:ADS_TO_REPLACE_1 <?php $a=12; if($a>... is the greatest Example 4: <?php $a=1;ADS_TO_REPLACE_12 switch
prepare statement
prepare statement  sir i want example of prepare statement
using switch case
using switch case  Define a class MENU to perform the following operation depending upon the users choice using switch case 1)print square root... choice: "); int choice=input.nextInt(); switch(choice
Switch case in Jsp page
Switch case in Jsp page  <table> <tr><td>Enter Distance :</td> <td><input type="text" name="dis"/></td>... using switch case in jsp page
difference between prepared statement and statement
difference between prepared statement and statement  i mean in prepared statement we write insert command as INSERT INTO tablename VALUES(?,?) but in normal statement we write insert into tablename(jtextfiled1.gettext
ModuleNotFoundError: No module named 'enum-switch'
ModuleNotFoundError: No module named 'enum-switch'  Hi, My Python... 'enum-switch' How to remove the ModuleNotFoundError: No module named 'enum-switch' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'e-switch'
ModuleNotFoundError: No module named 'e-switch'  Hi, My Python...-switch' How to remove the ModuleNotFoundError: No module named 'e-switch... to install padas library. You can install e-switch python with following
ModuleNotFoundError: No module named 'js.bootstrap-switch'
ModuleNotFoundError: No module named 'js.bootstrap-switch'  Hi, My... named 'js.bootstrap-switch' How to remove the ModuleNotFoundError: No module named 'js.bootstrap-switch' error? Thanks   Hi
ModuleNotFoundError: No module named 'mongo-switch'
ModuleNotFoundError: No module named 'mongo-switch'  Hi, My Python... 'mongo-switch' How to remove the ModuleNotFoundError: No module named 'mongo-switch' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nose-switch'
ModuleNotFoundError: No module named 'nose-switch'  Hi, My Python... 'nose-switch' How to remove the ModuleNotFoundError: No module named 'nose-switch' error? Thanks   Hi, In your python environment

Ads