使用Switch时为什么需要Break? [英] Why is Break needed when using Switch?

查看:153
本文介绍了使用Switch时为什么需要Break?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于SO的答案,但似乎无法找到一个明确的答案,为什么如果在这种情况下省略break,则C将始终被评估为true并且新数组将被填充只有G的。我清楚地明白,在这里最好使用break,因为我只是想评估一个特定的匹配,但不是为什么如果确实省略了break,为什么最后的情况总是为真。

I've read quite a few answers on SO but can't seem to find a definitive answer as to why if break is omitted in this instance the case "C" will always be evaluated as true and the new array filled will only "G"'s. I clearly understand that break would be best used here as I am only looking to evaluate a specific match but not so as to why the last case is always true if break is indeed omitted.

 var dna = "ATTGC";
 var outArr = [];
 dna.split("").forEach(function(e,i){
    switch(e) {
        case "G": outArr[i] = "C"; break;
        case "T": outArr[i] = "A"; break;
        case "A": outArr[i] = "T"; break;
        case "C": outArr[i] = "G"; break;
    }
    console.log(outArr);
  })


推荐答案

来自MDN


如果找到匹配项,程序执行相关的语句。如果多个案例与提供的值匹配,则选择匹配的第一个案例,即使案例彼此不相等。

If a match is found, the program executes the associated statements. If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.

与每个案例相关的可选break语句label确保一旦执行匹配语句,程序就会中断切换,并在切换后的语句处继续执行。如果省略break,程序将继续执行switch语句中的下一个语句。

The optional break statement associated with each case label 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.

所以关于 <的基本历史em> Switch 源于C,是分支表的摘要a>和分支表也有一个隐式的直通,需要一个额外的跳转指令来避免它。
此外,所有其他语言都继承切换为默认的C开关语义,大多选择保持默认的C语义。
因此,如果在所有情况下都忽略了break,那么在你的开关中,如果找到匹配,则程序继续执行下一个语句。找到匹配后它不会从开关退出,而是在匹配的情况下评估所有其他情况,因为解析器认为情况合并,因为它们之间没有中断
我已经修改了你的代码,以便清楚地解释这种行为。如果 A 中断,则省略。

So the basic history about Switch is it originated from C and is an abstraction of branch table and a branch table also has an implicit fall-through and requires an additional jump instruction to avoid it. Also all other languages inherited switch as default C switch semantics mostly choosing to keep the C semantics per default. So in your switch if break is omitted from all cases, if the match is found program continues execution for next statement. It doesn't exit from switch after the match is found instead it evaluates all other case below the matched case, as parser thinks cases are combined, since there is no break in between them. I have modified your code a bit for clear explanation of this behavior.In case A break is omitted.

var dna = "ATTGC";
var outArr = [];
dna.split("").forEach(function(e,i){
switch(e) {
    case "G": outArr[i] = "C"; break;
    case "T": outArr[i] = "A"; break;
    case "A": outArr[i] = "T";
    case "C": outArr[i] = "G"; break;
}
console.log(outArr);
})

所以你的输出就像

["G"]
["G", "A"]
["G", "A", "A"]
["G", "A", "A", "C"]
["G", "A", "A", "C", "G"]

In第一次迭代匹配将针对案例 A

In first iteration match will be for case A.

outArr={'T'}

再次由于没有 break 语句,它会将Case C 视为同一个块并继续执行。现在

Again Since there is no break statement, it will consider Case C as same block and continues execution there. Now

outArr={'G'}

Similalrly在第二次迭代中匹配案例 T 但是有一个 break 语句因此控件跳出开关并且outarr将现在

Similalrly in second iteration it matches for case T but there is a break statement so controls jumps out of the switch and outarr will now be

outArr={'G','A'}

同样对于其他迭代,您将得到上面公布的整个输出。

Similarly for other iterations you will get the whole output as posted above.


更新了Bin 此处

这篇关于使用Switch时为什么需要Break?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆