英文:
JavaScript - Code after case in switch statements
问题
关于您提到的代码中的问题,以下是翻译的部分:
这部分代码是在一个switch
语句中,但没有在任何case
或default
标签内。您想知道这部分代码何时执行。
这部分代码将会在以下情况下执行:
-
当
remainder
的值与case
标签中的值匹配时,相应的代码会被执行。例如,如果remainder
等于3,那么第一个case 3:
下的代码将执行,如果remainder
等于2,第二个case 2:
下的代码将执行,以此类推。 -
如果没有与
remainder
匹配的case
标签,这部分代码将不会执行。它不是一种替代default
语句的方式,而是根据remainder
的值来确定执行哪一段代码。
总之,这部分代码将根据remainder
的值选择性地执行相应的代码块,而不会像在switch
块外面的代码那样总是执行。
英文:
I am trying to decipher this code (MurmurHash) and came across the following lines:
switch (remainder) {
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1: k1 ^= (key.charCodeAt(i) & 0xff);
// When is this executed?
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
My question is as follows: I have never seen code inside a switch statement that is not part of either a case
or a default
and would greatly appreciate it if someone could explain when the part after the last case
statement is supposed to get executed.
Is it an alternative way of writing a default
statement?
Or will this always get executed, just as if it were written outside of the switch
block?
Information on this topic seems very difficult to come by as documentation on switch
statements generally deals with case
and default
, and it's also impossible to test without changing the code too much which might affect its behavior.
Thanks in advance!
答案1
得分: 3
以下是翻译好的代码部分:
这段代码是`case 1`的一部分。
个人觉得重新排列空白字符会更清晰:
switch (remainder) {
case 3:
k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2:
k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1:
k1 ^= (key.charCodeAt(i) & 0xff);
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
关键在于,在任何情况下都没有`break;`,所以任何匹配的情况都会执行,然后控制将流转到下一个情况。
所以,假设`remainder`只能是`1`、`2`或`3`...
- 如果是`3`,则执行所有语句。
- 如果是`2`,则跳过`case 3`,但执行其余部分。
- 如果是`1`,则跳过`case 3`和`case 2`,但执行其余部分。
这个逻辑,也许有点令人费解,依赖于`switch`的控制流继续到下一个(实际上不匹配的)`case`。
英文:
The code is part of case 1
.
Personally I'd re-arrange the whitespace to be more clear:
switch (remainder) {
case 3:
k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2:
k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1:
k1 ^= (key.charCodeAt(i) & 0xff);
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
The point is that, without a break;
in any of the cases, any matched case will execute and then control will flow to the next case.
So, assuming remainder
can only be 1
, 2
, or 3
...
- If it's
3
, all statements are executed. - If it's
2
,case 3
is skipped but the rest is executed. - If it's
1
,case 3
andcase 2
are skipped but the rest is executed.
The logic is, perhaps a bit unintuitively, relying on the control flow of switch
to continue on to the next (not-actually-matching) case
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论