switch 语句在 do while JavaScript 中

switch statement in do while javascript

本文关键字:JavaScript while do 语句 switch      更新时间:2023-09-26

我有以下c代码的转换。我尝试过的所有 c 编译器上的原始编译;海湾合作委员会,英特尔,视觉工作室。

然而,javascript

抱怨javascript中的do-while循环。

     var rindex=0;
     var lim=10;
     var res = 2;
     switch (res) {
        do {
            case 0: r[rindex] = uiColor; rindex++;
            case 3: r[rindex] = uiColor; rindex++;
            case 2: r[rindex] = uiColor; rindex++;
            case 1: r[rindex] = uiColor; rindex++;
        } while (rindex < lim);
    }

我似乎无法在不更改逻辑的情况下在 javascript 中找到一种方法来做到这一点。javascript 不允许在 switch 语句中执行 do 吗?

对,请参考 JavaScript 的 switch 语句语法。

switch (expression) {
  case value1:
    //Statements executed when the result of expression matches value1
    [break;]
  case value2:
    //Statements executed the result of expression matches value2
    [break;]
  ...
  case valueN:
    //Statements executed when the result of expression matches valueN
    [break;]
  default:
    //Statements executed when none of the values match the value of the expression
    [break;]
}

事实上,大多数语言都严格地定义了switch语句。以这种方式实现 Duff 的设备需要对 switch 语句进行宽松的规范。