switch statement

switch statement allows a program to evaluate an expression and attempt to match the expression‘s value to a case label. If a match is found, the program executes the associated statement. A switch statement looks as follows:

switch (expression) {
  case label_1:
    statements_1
    [break;]
  case label_2:
    statements_2
    [break;]
    ...
  default:
    statements_def
    [break;]
}

  

The program first looks for a case clause with a label matching the value of expression and then transfers control to that clause, executing the associated statements. If no matching label is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. By convention, the default clause is the last clause, but it does not need to be so.

The optional break statement associated with each case clause ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

Example

In the following example, if fruittype evaluates to "Bananas", the program matches the value with case "Bananas" and executes the associated statement. When break is encountered, the program terminates switch and executes the statement followingswitch. If break were omitted, the statement for case "Cherries" would also be executed.

switch (fruittype) {
  case "Oranges":
    console.log("Oranges are $0.59 a pound.");
    break;
  case "Apples":
    console.log("Apples are $0.32 a pound.");
    break;
  case "Bananas":
    console.log("Bananas are $0.48 a pound.");
    break;
  case "Cherries":
    console.log("Cherries are $3.00 a pound.");
    break;
  case "Mangoes":
    console.log("Mangoes are $0.56 a pound.");
    break;
  case "Papayas":
    console.log("Mangoes and papayas are $2.79 a pound.");
    break;
  default:
   console.log("Sorry, we are out of " + fruittype + ".");
}
console.log("Is there anything else you‘d like?");

  

时间: 2024-11-08 22:24:58

switch statement的相关文章

C++ basic - Switch statement

WHAT? 在写程序的时候我们会遇到很多种根据得出的数值就行不同处理的conditional statements,我们可以选用switch写法,以免使用太多的nested if会让程序difficult to read. syntax: switch(value) { case value1: ...; break; //break不能省略以免程序继续执行判断 case value2: ...; break; case value3: ...; break; default:...; } fo

ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.

1.L_Monitoring有这么些字段,ID,Collecttime,PlateType,PlateNO以及其他一些这段.建立这个表的时候是个非分区表,其中ID是主键,并在Collecttime,PlateType,PlateNO上面建立了索引. 2.系统运行一阵子后,L_Monitoring数据变得非常大,5,6千万,而且后续还会更大.所以要求将L_Monitoring表进行分区.分区方案是按照Collecttime进行每天分区.Collecttime为分区字段,并将Collecttime字

Partition table的switch条件2:Partition 的隐式Check约束 和Nullability

Partition column允许为Null,Null是最小值,存在于Partition Number=1的partition中. Any data with a NULL in the partition column will reside in the leftmost partition. NULL is considered smaller than the minimum value of the data type’s values. Partition Function 定义了

Partition table的switch条件1:结构相同(类型,nullability)

1,创建实例数据 -- create parition function CREATE PARTITION FUNCTION pf_int_Left (int) AS RANGE LEFT FOR VALUES (10,20); --create partition scheme CREATE PARTITION SCHEME PS_int_Left AS PARTITION pf_int_Left TO ([primary], [primary], [primary]); --create p

JavaScript.switch (true)

<!DOCTYPE html> <html> <head> <title>Switch Statement Example 4</title> <script type="text/javascript"> var num = 25; switch (true) { case num < 0: alert("Less than 0."); break; case num >= 0 &

Partition:分区切换(Switch)

在SQL Server中,对超级大表做数据归档,使用select和delete命令是十分耗费CPU时间和Disk空间的,SQL Server必须记录相应数量的事务日志,而使用switch操作归档分区表的老数据,十分高效,switch操作不会移动数据,只是做元数据的置换,因此,执行分区切换操作的时间是非常短暂的,几乎是瞬间完成,但是,在做分区切换时,源表和靶表必须满足一定的条件: 表的结构相同:列的数据类型,可空性(nullability)相同: 索引结构必须相同:索引键的结构,聚集性,唯一性,列

android官方技术文档翻译——switch 语句转换

本文译自androd官方技术文档<Switch Statement Conversion>,原文地址:http://tools.android.com/tips/non-constant-fields. 本文地址:http://blog.csdn.net/maosidiaoxian/article/details/41574853.转载请注明出处.翻译如有错讹,敬请指正. switch 语句转换 发布于 Sep 29, 2011, 2:35 PM 作者:Tor Norbye 自ADT 14起,

Using switch statements and the ternary operator

和JSswitch的语法一样? Switch Statements and the Ternary Operator are alternatives to if-else control structures for making decisions. The basic structure of a Switch Statement consists of the keyword switch followed by a pair of parentheses containing a va

javs switch 语句

//switch statement import java.util.Scanner; public class Sample { public static void main(String[] args) { int num; Scanner ip = new Scanner(System.in); System.out.print("Enter number between 1 to 4: "); num = ip.nextInt(); switch (num) { case