Switch Case in JAVA


Decision Making using switch case statement

Well you have seen the decision making in previos posts on if, if-else statements. switch case is yet another pwerful decision making concept.

We first ask the user to input some number, alphabet or some string to be used in taking decision so that control can be transferred at some relevant place in the program.

//JOptionPane.showInputDialog() gets only inputs string values, to make them numbers we need to convert it to integer as:

input = JOptionPane.showInputDialog(“Please enter 1, 2, or 3”);

//convert to integer

number=Integer.parseInt(input)

//for for inputting single character, we don’t convert string to letters, but read only first letter of string at position ‘0’. This is becaues strings are constructed using characters

input = JOptionPane.showInputDialog(“Please enter a, b, or c”);

// get first character in the string

letter = input.charAt(0);

// thus letter is the first character in a string. If user typed Ravinder then the first charater ‘R’ will be passed invariable letter.

Switch statement syntax requirement:

  • The switch statement starts with a keyword switch
  • You can pass either integer data type which short, long, byte or character variable. So it uses ordinal values which uses either a number or character.
  • Then we test for specific case.
  • Every case is terminated with a brak statements.
  • If there is no break statement in  a case, processing will continue with next case statement and so on.

switch (user expression)

{

case val-1:  statement(s);

     break;

case val-2:  statement(s);

    break;

default:   statement(s);

}

Here is an example:

User inputs a number, the number is then passed to the switch statement. Now depending on the user input program will go to case 1, 2 or 3 or the default.

package com.example;
import javax.swing.*;
public class switchCase
{

public static void main(String[] args)
{

char letter;
String input;
int number;
input= JOptionPane.showInputDialog(“Please Input a number”);
number = Integer.parseInt(input);
switch(number)

{

case 1:

JOptionPane.showMessageDialog(null, “You entered 1”);
break;

case 2:

JOptionPane.showMessageDialog(null, “You entered 2”);
break;

case 3:

JOptionPane.showMessageDialog(null, “You entered 3”);
break;

default:

JOptionPane.showMessageDialog(null, “You did not enter 1, 2 or 3”);

}

System.exit(0);
}
}

The last default statement in switch case is like an else part in if-else statement, and we test for a condition for the expression in each case, in this example we test 1, or 2 or 3 in different cases. There is no need to put break statement after the default case.

Example of chacater inputs in decision making

In this example a case is used without any break statement. example Case ‘A’ : and followed by case ‘a’: In such case first the Case ‘A’ is evaluated and then case ‘a’ is evaluated if any one of then is true then statement is printed as correct else next case will be evaluated. See example below:

package com.example;
import javax.swing.*;
public class switchCase
{
public static void main(String[] args)
{
char letter;

String input;

input = JOptionPane.showInputDialog(“Please enter a, b, or c”);
// get first character in the string
letter = input.charAt(0);
switch(letter)
{
case ‘A’:
case ‘a’ :

JOptionPane.showMessageDialog(null, “yu entered letter a or A”);
break;

case ‘B’:
case ‘b’ :

JOptionPane.showMessageDialog(null, “You entered letter b or B”);
break;

case ‘C’:
case ‘c’:

JOptionPane.showMessageDialog(null, “You enetered letter c or C”);
break;

default:

JOptionPane.showMessageDialog(null, “You did not enter letter a, or b or c”);

}

System.exit(0);
}
}

Remember that when a break statement is encountered, you come out of the  switch statement.

error: Content is protected !!