Posted By:
3 Feb, 2014 9:01 am PST

Using switch statement in Java

Using switch …case …default to simplify multiple if conditions

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax:

The syntax of switch statement is:

switch(expression){

    case value :

       //Statements

       break;//optional

    case value :

       //Statements

       break;//optional

    //You can have any number of case statements.

    default://Optional

       //Statements

}

 EXAMPLE:

 class Switch1{

public static void main(String [] args)

{

int number = 1234;

int remainder = number % 2;

switch ( remainder)

{

case 1:

System.out.println("Odd");

break;

case 0:

System.out.println("Even");

break;

}

}

}

 OUTPUT:

 

 Let us consider another example Day.java. Assume that the int variable day can have values 1, 2, 3, 4, 5, 6, 7, for Monday, Tuesday,…, Sunday. This means 6 stands for Saturday and 7 for Sunday. Create a file Day.java as shown below:

 class Day{

public static void main(String [] args)

{

int day = 6;

switch (day)

{

case 1:

case 2:

case 3:

case 4:

case 5:

System.out.println("Weekday");

break;

case 6:

case 7:

System.out.println("Weekend");

break;

}

}

}

 OUTPUT:

Change day = 6  to day = 4 and see what happens:

class Day1{

public static void main(String [] args)

{

int day = 4;

switch (day)

{

case 1:

case 2:

case 3:

case 4:

case 5:

System.out.println("Weekday");

break;

case 6:

case 7:

System.out.println("Weekend");

break;

}

}

} 

OUTPUT:

 EXPLANATION:

In the above example we have put 5 case values, one below the other, and then written the statements to be executed. This means that if the value of day  is any one of 1,2,3,4, or 5, the statements will be executed.

 Now, we will modify the program by removing the break statement. See below:

 class Day2{

public static void main(String [] args)

{

int day = 4;

switch (day)

{

case 1:

case 2:

case 3:

case 4:

case 5:

System.out.println("Weekday");

case 6:

case 7:

System.out.println("Weekend");

}

}

}

OUTPUT:

 

What has happened? In order to get out of a case block, we need to use the break statement. Otherwise, code will fall through the next case block automatically.

 The cascading concept with switch

What you have seen above is the cascading of the switch effect. If you do not use a break statement, then, once a matching code block gets executed, all the remaining code blocks will get executed, until a break statement is reached, or until the end of the switch statement.

The switch … case can also be used with char data types.

EXAMPLE:

class SwitchChar{

public static void main(String[] args)

{

char ch = 'B';

switch(ch)

{

case 'A':

System.out.println("A");

case 'B':

System.out.println("B");

case 'C':

System.out.println("C");

default:

System.out.println("Bye Bye");

}

}

}

OUTPUT:

Limitations of the switch statement

The switch statement can be used only with int or char data types. It cannot be used with String, float, long, double, or Boolean data types. Create a program file Ex0519.java as shown below:

class Ex0519{

public static void main(String[] args)

{

long wave = 4L;

switch (wave)

{

case 1:

System.out.println("1");

}

String s = "Two";

switch (s)

{

case "One":

System.out.println("1");

break;

case "Two":

System.out.println("2");

break;

case "Three":

System.out.println("3");

break;

}

}

}

Save the file and try to compile it. You will get the following error:

If you read the error messages, you will realize that the parameter to switch must be int only, or something that can be converted to int, such as char. Even long will not compile.

 

 



SWITCH STATEMENT

A switch statement is useful when you need to select one of several alternatives based on the value of an integer, a character, or a String variable. It is alternative to else-if ladder.

Syntax of switch statement:

switch (expression)
{
case constant:
statements;
break;
case constant-2:
statements;
break;..
default:
statements;
break;...
}

 

 

Example : To determine the number of players playing the game by switch statement.

 

public class Switch {
 
public static void main(String args[]){
 
int i=2;
switch (i) {
case 1 :
System.out.println("One player is playing this game.");
break;
case 2 :
System.out.println("Two players are playing this game.");
break;
case 3 :
System.out.println("Three players are playing this game.");
break;
default:
System.out.println("You did not enter a valid value.");
}
}
}

 

 

Was it useful?  



Flow statement that can be used is switch statement.That it  can have any number of possible executions.It allows user to run a block of statement from a selected code.

Syntax :

switch (expression)

{

  case constant:

        statements;

        break;

  case constant-2:

        statements;

        break;

  default:

        statements;

        break; 

}

 //To print the week day using switch.

import java.util.*;

public class SwitchExample{

            public static void main(String args[]){

                        System.out.println(“You have following choices!!”);

                        System.out.println(“1.Sunday \n 2.Monday \n 3.Wednesday \n 4.Thursday \n .                       5.Friday \n 6.Saturday”);

                        System.out.println(“Enter your choice:”);

                        Scanner scan = new Scanner(System.in);

                        int n=scan.nextInt();

 

                        switch(n){

                        case 1:

                                     System.out.println(“You entered Sunday as a week day!!”);

                        case 1:

                                     System.out.println(“You entered Monday as a week day!!”);

                       

                        case 2:

                                     System.out.println(“You entered Tuesay as a week day!!”);        

                        case 3:

                                     System.out.println(“You entered Wednesday as a week day!!”);                   

                        case 4:

                                     System.out.println(“You entered Thursday as a week day!!”);                   

                        case 5:

                                     System.out.println(“You entered Friday as a week day!!”);

                        case 6:

                                     System.out.println(“You entered Ssturday as a week day!!”);

                        default:

                                    System.out.println(“Wrong choice!!”);

                        }

            }

}         

 

OUTPUT:

You have following choices!!

 

1.Sunday

2.Monday

3.Tuesday

4.Wednesday

5.Thursday

6.Friday

7.Saturday

 

Enter your choice:5

You entered Thursday as a week day!!


Was it useful?  



Using switch …case …default to simplify multiple if conditions

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax:

The syntax of switch statement is:

switch(expression){

    case value :

       //Statements

       break;//optional

    case value :

       //Statements

       break;//optional

    //You can have any number of case statements.

    default://Optional

       //Statements

}

 

 EXAMPLE:

 

class Switch1{

public static void main(String [] args)

{

int number = 1234;

int remainder = number % 2;

switch ( remainder)

{

case 1:

System.out.println("Odd");

break;

case 0:

System.out.println("Even");

break;

}

}

}

 

 

OUTPUT:

 

Let us consider another example Day.java. Assume that the int variable day can have values 1, 2, 3, 4, 5, 6, 7, for Monday, Tuesday,…, Sunday. This means 6 stands for Saturday and 7 for Sunday. Create a file Day.java as shown below:

 

class Day{

public static void main(String [] args)

{

int day = 6;

switch (day)

{

case 1:

case 2:

case 3:

case 4:

case 5:

System.out.println("Weekday");

break;

case 6:

case 7:

System.out.println("Weekend");

break;

}

}

}

 

 

OUTPUT:

Change day = 6  to day = 4 and see what happens:

 

class Day1{

public static void main(String [] args)

{

int day = 4;

switch (day)

{

case 1:

case 2:

case 3:

case 4:

case 5:

System.out.println("Weekday");

break;

case 6:

case 7:

System.out.println("Weekend");

break;

}

}

} 

 

 

OUTPUT:

 

EXPLANATION:

In the above example we have put 5 case values, one below the other, and then written the statements to be executed. This means that if the value of day  is any one of 1,2,3,4, or 5, the statements will be executed.

Now, we will modify the program by removing the break statement. See below:

 

class Day2{

public static void main(String [] args)

{

int day = 4;

switch (day)

{

case 1:

case 2:

case 3:

case 4:

case 5:

System.out.println("Weekday");

case 6:

case 7:

System.out.println("Weekend");

}

}

}

 

 

OUTPUT:

What has happened? In order to get out of a case block, we need to use the break statement. Otherwise, code will fall through the next case block automatically.

 

The cascading concept with switch

What you have seen above is the cascading of the switch effect. If you do not use a break statement, then, once a matching code block gets executed, all the remaining code blocks will get executed, until a break statement is reached, or until the end of the switch statement.

The switch … case can also be used with char data types.

 

EXAMPLE:

 

class SwitchChar{

public static void main(String[] args)

{

char ch = 'B';

switch(ch)

{

case 'A':

System.out.println("A");

case 'B':

System.out.println("B");

case 'C':

System.out.println("C");

default:

System.out.println("Bye Bye");

}

}

}

 

 

OUTPUT:

 

Limitations of the switch statement

The switch statement can be used only with int or char data types. It cannot be used with String, float, long, double, or Boolean data types. Create a program file Ex0519.java as shown below:

 

class Ex0519{

public static void main(String[] args)

{

long wave = 4L;

switch (wave)

{

case 1:

System.out.println("1");

}

String s = "Two";

switch (s)

{

case "One":

System.out.println("1");

break;

case "Two":

System.out.println("2");

break;

case "Three":

System.out.println("3");

break;

}

}

}

 

 

Save the file and try to compile it. You will get the following error:

If you read the error messages, you will realize that the parameter to switch must be int only, or something that can be converted to int, such as char. Even long will not compile.


Was it useful?  




Please login to reply to this problem.