switch case语句_使用Case(Switch)Ruby语句 2022-12-06 15:44 198阅读 0赞 switch case语句 In most [computer languages][], the case or conditional (also known as [*switch*][switch]) statement compares the value of a variable with that of several constants or literals and executes the first path with a matching case. In [Ruby][], it's a bit more flexible (and powerful). 在大多数[计算机语言中][computer languages] ,case或条件语句(也称为[*switch*][switch] )将变量的值与几个常量或文字的值进行比较,并以匹配的case执行第一个路径。 在[Ruby中][Ruby] ,它更加灵活(功能强大)。 Instead of a simple equality test being performed, the case equality operator is used, opening the door to many new uses. 代替执行简单的相等性测试,而是使用案例相等性运算符,这为许多新用途打开了大门。 There are some differences from other languages though. In [C][], a switch statement is a kind of replacement for a series of *if and goto* statements. The cases are technically labels, and the [switch statement][] will go to the matching label. This exhibits a behavior called "fallthrough," as the execution doesn't stop when it reaches another label. 但是与其他语言有些区别。 在[C中][C] ,switch语句是一系列*if和goto*语句的一种替代。 这些案例在技术上是标签,并且[switch语句][switch statement]将转到匹配的标签。 这表现出一种称为“ fallthrough”的行为,因为当到达另一个标签时执行不会停止。 This is usually avoided using a break statement, but fallthrough is sometimes intentional. The case statement in Ruby, on the other hand, can be seen as a shorthand for a series of *if* statements. There is no fallthrough, only the first matching case will be executed. 通常使用break语句可以避免这种情况,但是有时会故意失败。 另一方面,Ruby中的case语句可以看作一系列*if*语句的简写。 没有失败,只会执行第一个匹配的大小写。 ## 案件陈述书的基本形式 **(** The Basic Form of a Case Statement **)** ## The basic form of a case statement is as follows. case语句的基本形式如下。 As you can see, this is structured something like an if/else if/else conditional statement. The name (which we'll call the *value*), in this case inputted from the keyboard, is compared to each of the cases from the **when** clauses (i.e. *cases*), and the first when block with a matching case will be executed. If none of them match, the **else** block will be executed. 如您所见,它的结构类似于if / else if / else条件语句。 在这种情况下,将从键盘输入的名称(我们将其称为*value* )与**when**子句(例如, *case* )中的每个case进行比较,并将执行第一个带有匹配case的when块。 如果它们都不匹配,则将执行**else**块。 What's interesting here is *how* the value is compared to each of the cases. As mentioned above, in [C++][C 1], and other C-like languages, a simple value comparison is used. In Ruby, the case equality operator is used. 这里有趣的是*如何*将值与每种情况进行比较。 如上所述,在[C ++][C 1]和其他类似C的语言中,使用了简单的值比较。 在Ruby中,使用大小写相等运算符。 Remember that the type of the left-hand side of a case equality operator is important, and the cases are always the left-hand side. So, for each **when** clause, Ruby will evaluate **case === value** until it finds a match. 请记住,案例相等性运算符左侧的类型很重要,案例始终为左侧。 因此,对于每个**when**子句,Ruby都会评估**case ===的值,**直到找到匹配项为止。 If we were to input **Bob**, Ruby would first evaluate **"Alice" === "Bob"**, which would be false since *String\#===* is defined as the comparison of the strings. Next, **/\[qrz\].+/i === "Bob"** would be executed, which is false since **Bob** doesn't begin with Q, R or Z. 如果我们要输入**Bob** ,那么Ruby首先会评估**“ Alice” ===“ Bob”** ,因为*String#===*被定义为字符串的比较,所以它是错误的。 接下来,将执行**/\[qrz\].+/i ===“ Bob”** ,这是错误的,因为**Bob的**开头不是Q,R或Z。 Since none of the cases matched, Ruby will then execute the else clause. 由于所有情况都不匹配,因此Ruby将执行else子句。 ## 类型如何发挥作用 **(** How the Type Comes Into Play **)** ## A common use of the case statement is to determine the type of value and do something different depending on its type. Though this breaks Ruby's customary duck typing, it's sometimes necessary to get things done. case语句的常见用法是确定值的类型,并根据其类型执行不同的操作。 尽管这打破了Ruby惯用的鸭子输入方式,但有时还是有必要将其完成。 This works by using the **Class\#===** (technically, the **Module\#===**) operator, which tests if the right-hand side **is\_a?** left-hand side. 这可以通过使用**Class#===** (在技术上为**Module#====** )运算符来实现,该运算符将测试右侧的**is\_a吗?** 左手边。 The syntax is simple and elegant: 语法简单而优雅: ## 另一种可能的形式 **(** Another Possible Form **)** ## If the *value* is omitted, the case statement works a bit differently: it works almost exactly like an if/else if/else statement. The advantages of using the case statement over an *if* statement, in this case, are merely cosmetic. 如果省略该*值* ,则case语句的工作方式略有不同:它的工作方式几乎完全类似于if / else if / else语句。 使用case语句在一个*if*语句,在这种情况下的优点,仅仅是化妆品。 ## 更紧凑的语法 **(** A More Compact Syntax **)** ## There are times when there are a large number of small **when** clauses. Such a case statement easily grows too large to fit on the screen. When this is the case (no pun intended), you can use the **then** keyword to put the body of the **when** clause on the same line. 有时候,会有大量的小**when**子句。 这样的案例陈述很容易变得太大而无法容纳在屏幕上。 在这种情况下(不需要双关语),可以使用**then**关键字将**when**子句的正文放在同一行上。 While this makes for some very dense code, as long as each **when** clause is very similar, it actually becomes *more* readable. 尽管这需要一些非常密集的代码,但只要每个**when**子句非常相似,它实际上就会变得*更具*可读性。 When you should use single-line and multi-line when clauses are up to you, it's a matter of style. However, mixing the two is not recommended - a case statement should follow a pattern to be as readable as possible. 当您应该使用单行和多行的when子句时,这是样式问题。 但是,不建议将两者混为一谈-case语句应遵循一定的模式,以使其更具可读性。 ## 案例分配 **(** Case Assignment **)** ## Like if statements, case statements evaluate to the last statement in the **when** clause. In other words, they can be used in assignments to provide a kind of table. However, don't forget that case statements are much more powerful than simple array or hash lookups. Such a table doesn't necessarily need to use literals in the **when** clauses. 与if语句一样,case语句的计算结果为**when**子句中的最后一条语句。 换句话说,它们可以用于分配中以提供一种表。 但是,不要忘记case语句比简单的数组或哈希查找功能强大得多。 这样的表不一定需要在**when**子句中使用文字。 If there is no matching when clause and no else clause, then the case statement will evaluate to **nil**. 如果没有匹配的when子句和else子句,则case语句的计算结果为**nil** 。 > 翻译自: [https://www.thoughtco.com/case-switch-statement-2907913][https_www.thoughtco.com_case-switch-statement-2907913] switch case语句 [computer languages]: https://www.thoughtco.com/what-is-a-programming-language-958332 [switch]: https://www.thoughtco.com/conditional-statements-2034048 [Ruby]: https://www.thoughtco.com/what-is-ruby-2907828 [C]: https://www.thoughtco.com/c-for-beginners-958273 [switch statement]: https://www.thoughtco.com/using-the-switch-statement-for-multiple-choices-2033886 [C 1]: https://www.thoughtco.com/candand-for-beginners-958278 [https_www.thoughtco.com_case-switch-statement-2907913]: https://www.thoughtco.com/case-switch-statement-2907913
相关 switch case语句_使用Case(Switch)Ruby语句 switch case语句 In most [computer languages][], the case or conditional (also known as [s ╰半夏微凉°/ 2022年12月06日 15:44/ 0 赞/ 199 阅读
相关 php 的switch case语句吗,PHP switch case语句 PHP 中的条件控制语句有两个,一个是 if else 语句,另一个是 switch case 语句。上节讲解了 if else,本节接着讲解 switch case 语句。 男娘i/ 2022年11月17日 03:38/ 0 赞/ 158 阅读
相关 React 项目使用 switch ... case ... 语句 最近项目中需要使用到switch...case...语句,但不知道在React中怎么使用,特此记录便于日后查阅。 但是不得不说 switch 末蓝、/ 2022年08月29日 15:54/ 0 赞/ 112 阅读
相关 c#——switch case语句 c\——switch case语句 > c\中的switch case语句有三种结构,具体形式如下图所示: ![0_133110889753v5.gif][] > Bertha 。/ 2022年08月26日 04:46/ 0 赞/ 235 阅读
相关 switch...case语句 public static int getValue(int i) { int result = 0; switch ( ゝ一纸荒年。/ 2022年08月09日 06:52/ 0 赞/ 222 阅读
相关 java switch case 语句 基本语句格式 switch(参数){ case 变量值1: 代码块语句; break; case 变量值2: 代码块语句; 爱被打了一巴掌/ 2022年06月10日 00:11/ 0 赞/ 277 阅读
相关 switch-case语句 switch语句可以看作是一种基于计算的跳转,根据后面括号里的值匹配,程序会跳转到相匹配的case处。 在执行完分支中的最后一条语句后,如果后面没有break,就会顺序执行到 ﹏ヽ暗。殇╰゛Y/ 2022年05月31日 11:16/ 0 赞/ 198 阅读
相关 java switch case 语句 switch case语句用来判断一个变量与一系列值中的某个值是否相等,每一个值成为一个分支 switch(expression)\{ case va 爱被打了一巴掌/ 2022年04月22日 02:54/ 0 赞/ 275 阅读
相关 golang switch case语句 简介 注意: 1.switch/case 后是一个表达式(即:常量,变量,一个有返回的函数都可以); 2.case后的各个表达式的值的数据类型,必须和switch的表达式数据类型 短命女/ 2021年09月07日 06:10/ 0 赞/ 403 阅读
相关 Java switch case 语句 switch case 语句有如下规则: - switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,swit... 小灰灰/ 2020年05月20日 14:24/ 0 赞/ 2033 阅读
还没有评论,来说两句吧...