for-loop inside switch statement

107 Views Asked by At

I was converting a c++ code I found over the internet to javascript, and I was perplexed to see the following statement:

switch(wheelIndex) {
  for(;;) {  //<--------
    case 0:
      ...
    case 1:
      ...
  }
  break;
  for(;;) {
    ...

What does for(;;) inside switch mean?

1

There are 1 best solutions below

0
On BEST ANSWER

That is a very old (dirty) C trick commonly referred as Duff's device. The idea is that the switch let you jump at a some place inside a loop.

So basically you have a loop but you can start it at some defined points (the cases), that on the value wheelIndex.

The loop itself is an infinite loop (no initialization, no test, no increment). This kind of loop can be broken by the use of some goto, break or return.