Switch statements | Decision making, Branching and Looping

Switch statements Flow control with if and else statements gets cumbersome when a variable must be tested for a large number of possible values, such as a menu selection that permits the user to enter an integer from 1 to 20. The solution to this problem is the switch statement.

The switch statement

1) Specifies an expression whose value is be tested. This is known as the “switch expression” and is typically the name of a single variable.

2) Defines a number of “cases”, each associated with a different value. There may also be a “default” case that is not associated with a value. If the value of the switch expression is equal to the value of a case, the statements for that case will be executed. Otherwise, the statements of the default case will be executed.

3) General syntax:

switch (expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
break;
}

It has a number subtleties and restrictions:

1. The switch expression must be a primitive of type int or able to be promoted to int. Specifically, it must be either byte, short, int, or char. Expressions of type boolean, long, float, and double will not compile.

2. The value specified on a case must be a constant of type int or must be able to be promoted to int (in other words a byte, short, int, or char). It must also be within the possible range of values of the switch expression. For example, if the switch variable is a byte, a case with a value of 200 would not compile because a byte may only have a value from -128 to 127.

The value of a case may be an expression as long as the result is a constant.

For example,
case 5 + 1:
would compile successfully.

3. The break statement is optional and will be covered in more detail in a later lesson. When encountered, it ends the execution of the switch statement. If omitted from a case, processing falls through to the next case (a sometimes undesirable result).

4. Although the compiler doesn’t care, cases should be arranged in a high probability to low probability order to enhance processing efficiency. The default can be placed anywhere (even first).

It may be nested.
A switch can be coded within another switch or in either leg of an if-else.
It can also contain if-else code.

Example:

import java.util.*;
import java.io.*;
import java.lang.*;
public class Aman
{
public static void main(String[] args)
{

// Variables
// Prompt for and read data

System.out.print(“Enter customer’s starting balance: “);
Scanner Keyboard=new Scanner(System.in);
Double balance;
balance = Keyboard.readDouble();
System.out.print(“Enter transaction amount: “);
double amount;
amount = Keyboard.readDouble();
System.out.println(“Transaction codes are”);
System.out.println(” ” + “C – charge”);
System.out.println(” ” + “P – payment”);
System.out.println(” ” + “R – refund or return”);
System.out.print(“Enter transaction code: “);
char code;
code = Keyboard.readChar();

// Process based upon transaction code

switch (code)
{
case ‘C’:
case ‘c’:
balance += amount;
System.out.println(“New balance is ” + Utility.moneyFormat(balance));
break;
case ‘P’:
case ‘p’:
balance -= amount;
System.out.println(“New balance is ” + Utility.moneyFormat(balance));
break;
case ‘R’:
case ‘r’:
balance -= amount;
System.out.println(“New balance is ” + Utility.moneyFormat(balance));
break;
default:
System.out.println(“Invalid transaction code”);
break;
}
}
}
***try it with the help of under given suggestion.

Notes:

1. The balance, code, and amount variables hold data entered by the user.

2. After all data has been read from the user, the transaction is processed by the switch statement with the transaction code (code) used as the switch expression.

3. Within the switch, if the value of code matches the value of a particular case, processing jumps to the statement block for that case, otherwise processing jumps to the statement block for the default. Processing will then continue until either a break statement is encountered or the end of the switch is reached.

4. Stacking two or more cases without at break statement constitutes an OR.

For example,
case ‘R’:
case ‘r’:

will result in the same processing being performed if the value of code is either an uppercase or lowercase ‘R’.

5. For transactions with a valid transaction code, the customer’s new balance is displayed using the moneyFormat() method of my Utility class. If the transaction code is bad, an error message is displayed.