简体   繁体   English

如何在循环中使用switch语句?

[英]How to use switch statement inside for loop?

 for(var i=0; i<20; i++) { if(i%3===0) { console.log(i, 'Foo') } else { console.log(i, 'Default') } } 

Now, I wonder how can we write the code using switch statement inside the loop: 现在,我想知道如何在循环中使用switch语句编写代码:

 for(var i=0; i<20; i++) { switch(i) { case (i%3===0): console.log(i,'Foo') break default: console.log(i,'Default') break } } 

But it results 'Default' always. 但它总是导致'默认'。 I have tried using label, anonymous function, etc. but not able to output like if condition. 我曾尝试使用标签,匿名函数等,但无法像条件一样输出。 Am I doing something wrong with the switch statment? 我是否对开关声明做错了什么?

Edit: 编辑:

I was trying to do like this in fact: 事实上我试图这样做:

case (i%3===0):
   console.log(i,'Foo')
   break
case (i%5===0):
   console.log(i,'Bar')
   break

You are trying to use a switch statement like a series of if and else if statements. 您正在尝试使用switch语句,如一系列ifelse if语句。 A switch statement does not work that way. switch语句不起作用。 The first case that matches the value of the variable that is in the switch statement will be evaluated. 将评估与switch语句中的变量值匹配的第一种情况。 You can use switch(true) so the first case that is true will be evaluated. 您可以使用switch(true)以便评估第一个为true的情况。

 for(var i=0; i<20; i++) { switch(true) { case (i%3===0)://if console.log(i,'Foo') break case (i%5===0)://else if console.log(i,'Bar') break default://else console.log(i,'Default') break } } 

Otherwise, you need to switch the value of i modulo 3 (if it equals zero then it is divisible by 3 ). 否则,你需要切换i modulo 3的值(如果它等于零则可以被3整除)。

 for(var i=0; i<20; i++) { switch(i%3) { case (0): console.log(i,'Foo') break default: console.log(i,'Default') break } } 

However, a switch statement generally should not be used in this case. 但是,在这种情况下通常不应使用switch语句。 You should just go with a series of if (and else if statements). 你应该使用一系列if (以及else if语句)。

 for(var i=0; i<20; i++) { if(i%3==0){ console.log(i, 'Foo'); } else if(i%5==0){ console.log(i, 'Bar'); } else { console.log(i, 'Default'); } } 

You can take the value of i%3 in a variable and use that in switch-case because the case evaluates a constant or expression. 您可以在变量中获取i%3的值,并在switch-case使用它,因为该case计算常量或表达式。

 for(var i=0; i<20; i++) { var val = i%3; switch(val) { case 0: console.log(i,'Foo') break; default: console.log(i,'Default') } } 

You should switch the expression you want to check against, in this case that's i%3 and than make cases for what that expression might be, eg case 0: , case 1: and so on: 您应该切换要检查的表达式,在这种情况下是i%3而不是表达该表达式的case 0: ,例如case 0: , case 1:依此类推:

 for(var i=0; i<20; i++) { switch(i%3) { case 0: console.log(i,'Foo') break; case 1: console.log(i,'Bar') default: console.log(i,'Default') } } 

try this - with the %3 moved 试试这个 - 移动了%3

 for(var i=0; i<20; i++) { switch(i%3) { case 0: console.log(i,'Foo') break; default: console.log(i,'Default') break; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM