JavaScript – switch语句中case之后的代码部分

huangapple go评论58阅读模式
英文:

JavaScript - Code after case in switch statements

问题

关于您提到的代码中的问题,以下是翻译的部分:

这部分代码是在一个switch语句中,但没有在任何casedefault标签内。您想知道这部分代码何时执行。

这部分代码将会在以下情况下执行:

  1. remainder的值与case标签中的值匹配时,相应的代码会被执行。例如,如果remainder等于3,那么第一个case 3:下的代码将执行,如果remainder等于2,第二个case 2:下的代码将执行,以此类推。

  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) &amp; 0xff) &lt;&lt; 16;
case 2:
k1 ^= (key.charCodeAt(i + 1) &amp; 0xff) &lt;&lt; 8;
case 1:
k1 ^= (key.charCodeAt(i) &amp; 0xff);
k1 = (((k1 &amp; 0xffff) * c1) + ((((k1 &gt;&gt;&gt; 16) * c1) &amp; 0xffff) &lt;&lt; 16)) &amp; 0xffffffff;
k1 = (k1 &lt;&lt; 15) | (k1 &gt;&gt;&gt; 17);
k1 = (((k1 &amp; 0xffff) * c2) + ((((k1 &gt;&gt;&gt; 16) * c2) &amp; 0xffff) &lt;&lt; 16)) &amp; 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 and case 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.

huangapple
  • 本文由 发表于 2023年6月2日 03:22:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定