在javascript中执行切换语句

时间:2015-01-20 20:21:08

标签: javascript switch-statement do-while

我有以下c代码转换。原始编译我试过的所有c编译器; gcc,intel,Visual Studios。

然而,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语句中执行操作?

1 个答案:

答案 0 :(得分:1)

是的,请参阅JavaScript的switch statement syntax

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的设备需要relaxed specification of the switch statement