2 ms·
But that, like most uses of switch, is essentially uninteresting as a control-flow construct. It's just a stylistic point to use a switch here instead of an if
by edflsafoiewq 7y ago
But that, like most uses of switch, is essentially uninteresting as a control-flow construct. It's just a stylistic point to use a switch here instead of an if or a match. JS's switch has no extra power other than fallthrough anyway.
Here's a poor-man's coroutine (with serializable state!) where switch plays a more interesting role.
void move_robot(state* s) {
switch (s->resume_at) {
case 0:
#define SUSPEND(resume_pt) s->resume_at = resume_pt; return; case resume_pt:;
#define PAUSE(resume_pt) s->pause_time = 20; while (s->pause_time) { SUSPEND(resume_pt) }
for (;;) {
/* three knight moves, then forward */
for (s->i = 0; s->i != 3; s->i++) {
forward(s);
PAUSE(1);
forward(s);
PAUSE(2);
turn_left(s);
forward(s);
PAUSE(3);
}
forward(s);
}
}
}