4 ms·
I have the following pattern two or three times before. When it comes up, it's very useful. It can eliminate code repetition, without the bother of making a wh
by kazinator 6y ago
I have the following pattern two or three times before. When it comes up, it's very useful.
It can eliminate code repetition, without the bother of making a whole new function:
for(i = 0; i < 10; i++){
switch (i) {
case 1:
// this is special for 1
break;
case 3:
// this is special for 3
// fallthrough
case 5:
// case 5 needs 3 processing, plus its own
break;
case ...:
// ...
}
// a slightly long block here
// common to all cases.
}
I don't think I've ever done anything quite like this though:
switch (i) {
case 0: // so we can enter the for at all
for (i = 0; i < 10; i++) {
case 1: ;
}
}
which is what the parent comment is getting at, if taken literally.