The selection operator ‘switch’

The selection operator ‘switch’


Content
  1. What purpose does the ‘switch’ operator have in the programs?
  2. What is the general form of the ‘switch’ statement?
  3. The principle of operation of the switch statement
  4. An example of using the switch operator, which has a default block
  5. An example of using the operator ‘switch’, in which the operators ‘break’ are absent
  6. An example of using the operator ‘switch’, in which there is no block ‘default’

1. What purpose does the ‘switch’ operator have in the programs?

The ‘switch’ operator is close to the conditional if-else statement. It allows you to organize the branching of the execution process in the program. In some cases, the use of the ‘switch’ operator gives a more compact program code than the conditional if-else statement.

 

2. What is the general form of the ‘switch’ statement?

In C-oriented programming languages, to which Java belongs, the ‘switch’ operator has the same general form of representation:

switch (expression)
{
    case value1:
        // statements
        ...
    break;
    case value2:
        // statements
        ...
    break;

    ...

    case valueN:
        // statements
        ...
    break;
    default:
        // operators that are executed by default
        ...
}

where

  • expression – a control expression that can have one of integer types: byte, int, short, char, or enumeration type. In new versions of JDK, the expression can be of type String.
  • value1, value2, …, valueN – constant expressions (literal values). Each value must be type-compatible with the specified expression.

The ‘default’ block can be absent.

 

3. The principle of operation of the switch statement

The operator of choice ‘switch’ works according to the following principle. The value of the expression is compared with any of the values (value1, value2, …, valueN) that follow after the keyword ‘case’. If a match is found, then the operators that follow this part of the ‘case’ statement are executed. If no match is found after “case”, then the operators that are in the “default” block are executed. If the ‘default’ block is missing (the block may be missing), then nothing happens and the next statement that follows the ‘switch’ statement is executed.

The ‘break’ statement is required to immediately exit the ‘switch’ statement. If the ‘break’ statement is executed, then the operator following the ‘switch’ is executed.

 

4. An example of using the switch operator, which has a default block

For a given value of n = 1..7 display the name of the corresponding day of the week. It is needed to consider the possible error values of n.

// the selection operator 'switch'
int n;
String s;

// input of the value n
n = 6;

switch (n)
{
    case 1:
        s = "Sunday";
    break;
    case 2:
        s = "Monday";
    break;
    case 3:
        s = "Tuesday";
    break;
    case 4:
        s = "Wednesday";
    break;
    case 5:
        s = "Thursday";
    break;
    case 6:
        s = "Friday";
    break;
    case 7:
        s = "Saturday";
    break;
    default:
        s = "Invalid day";
}

System.out.println(s);

 

5. An example of using the operator ‘switch’, in which the operators ‘break’ are absent

As you know, the break statement can be absent.

In this example, according to the entered day number value n = 1..7, the day off or work day is determined.

// the selection operator 'switch'
int n;
String s;

// ввод значения n
n = 3;

switch (n)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        s = "Working day";
    break;
    case 6:
    case 7:
        s = "Day off";
    break;
    default:
        s = "Invalid day";
}

System.out.println(s);

 

6. An example of using the operator ‘switch’, in which there is no block ‘default’

Given an integer n = 1..3. For a given value of variable n to determine:

  1. circumference;
  2. area of a circle;
  3. volume of a sphere.
// the selection operator 'switch'
int n;
double pi = 3.1415;
double r;

// input of the values n, r
// ...

switch (n)
{
    case 1:
        double d = 2*pi*r;
        System.out.println("Circumference = " + d);
    break;
    case 2:
        double s = pi*r*r;
        System.out.println("Area of a circle = " + s);
    break;
    case 3:
        double v = 4.0/3.0 * pi*r*r*r;
        System.out.println("Volume of a sphere = " + v);
    break;
}

 


Related topics