JavaScript. Statement switch

JavaScript. Statement switch


Contents


Search other resources:

1. Statement switch. General concepts

The switch statement allows you to select one option from the proposed set. In the switch statement, the current value of a variable or expression is compared for equality with one of the possible options, and, in case of equality, the corresponding program code is executed.

The syntax of the switch statement is as follows

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

  ...

  case valueN:
    // Instructions_N
    // ...
  break;
  default:
    // Instructions_N+1
  break;
}

here

  • expression – some expression or variable value;
  • value1, value2, valueN – the value with which the expression is compared. These values can be of numeric, string, character, and boolean types;
  • Instructions_1, Instructions_2, Instructions_N – operators (instructions) executed if the result of expression matches according to value1, value2, valueN;
  • Instructions_N+1—operators (instructions) to be executed if no match is found.

The block of statements defined by the keyword default may be missing.

Break statements after each statement block provide an exit from the case statement. If you remove the break statements, then the case statement will not exit and the execution of the following statements will continue. For example, in the following fragment, all statements are executed in succession, starting with the statement that displays the number 2.5.

var num = 2.5

switch (num) {
  case 1.5: console.log("1.5")
  case 2.5: console.log("2.5") // then all consecutive statements are executed
  case 3.5: console.log("3.5")
  case 4.5: console.log("4.5")
}

 

2. An example of processing the result of an expression that has an integer numeric type

A code fragment is shown that displays the name of this day for a given number of the day of the week

// The switch statement
// The number of the day of the week is set
var day = 3

// Display the name of the day of the week by the given number
switch (day) {
  case 1: console.log("Monday");
  break;
  case 2: console.log("Tuesday");
  break;
  case 3: console.log("Wednesday");
  break;
  case 4: console.log("Thursday");
  break;
  case 5: console.log("Friday");
  break;
  case 6: console.log("Saturday");
  break;
  case 7: console.log("Sunday");
  break;
  default:
    console.log("Incorrect day")
  break;
}

Result

Wednesday

 

3. An example of processing a string type expression

The name of the country is set. The name indicates the capital of this country.

// switch statement
// Processing an expression containing a string
var country = "Ukraine"

// Given the name of the country, display the name of its capital
switch (country) {
  case "USA": console.log("Washington");
  break;
  case "Slovakia": console.log("Bratislava");
  break;
  case "Ukraine": console.log("Kiev");
  break;
  case "Belgium": console.log("Brussels");
  break;
  case "Ireland": console.log("Dublin");
  break;
  default:
    console.log("Another country");
  break;
}

Result

Kiev

 

4. An example of the implementation of an operation on a given string

The example specifies an operation string. Based on the string, the corresponding statement is executed.

// switch statement
// Processing an expression containing the designation of an operation
var symbol = '*'
var x = 5, y = 8, z

// Based on the value of the symbol, implement the corresponding operation
switch (symbol) {
  case '+': z = x + y;
  break;
  case '-': z = x - y;
  break;
  case '*': z = x * y;
  break;
  case '/': z = x / y
  break;
  default:
    z = 0
  break;
}

console.log("z = " + z)

Result

z = 40

 

5. An example of determining the characteristic of the corresponding figure based on the operation number

In the example, based on the given figure number and radius, the corresponding characteristic of the figure is calculated. The result is displayed on the screen.

// switch statement
// Processing characteristics of a geometric figure

// Is given
var numFigure = 2
var radius = 3.0

switch (numFigure) {
  case 1:
    // Calculate the circumference
    var length = 2 * Math.PI * radius
    console.log("length = " + length)
  break;
  case 2:
    // Calculate the area of a circle
    var area = Math.PI * radius * radius
    console.log("area = " + area)
  break;
  case 3:
    // Calculate the volume of a sphere
    var volume = 4.0 / 3 * Math.PI * radius * radius * radius
    console.log("volume = " + volume);
  break;
  default:
    console.log("Undefined figure");
}

Result

area = 28.274333882308138

 


Related topics