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:
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
It is not necessarily an antipattern to use a switch statement within a loop—it is only considered incorrect when used to model a known sequence of steps. The most common example of the correct use of a switch within a loop is an inversion of control such as an event handler.
When numbers are iterated in the loop from 1 to 9, they are being conditionally tested with the switch cases starting from the top. As an example when number = 1 it will print One and so on. Only after the first switch case condition is not satisfied the program checks for the next switch case condition.
Yes you can, though for anything other than very short loops it can rapidly make your code unreadable. In such cases it's better to put the loop into a function and call it from the case.
Yes you can call switch in if . you can not define a function inside another function.
The switch/case statement inside of a for loop is fine, the problem is that switch isn't a function call. Try something like this: Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement, and no other following command in the same case or loop is executed. A switch statement can have an optional default case as well at the last to give the default condition to be executed.
There can be various switch-case statements within a switch. Each case is followed by the value to be compared to. When the variable being switched on is equal to its corresponding case, the statements following that case will execute until a break statement is reached.
What is the difference between loop and switch cases? A loop often works on members of an array or vector, or perhaps each key in a map. In such cases the loop means “do the same thing to all the elements in this structure, one at a time, one after the other”.
You are trying to use a switch
statement like a series of if
and else if
statements. A switch statement does not work that way. The first case that matches the value of the variable that is in the switch statement will be evaluated. You can use switch(true)
so the first case that is true will be evaluated.
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
).
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. You should just go with a series of if
(and else if
statements).
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');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With