`switch` and `continue` cooperation

Really small tip, but (hopefully) is worth saying about it. Your switch operator won't work with continue, but if only your current `switch` operator is within the loop. This means, that first piece of code won't compile, but the last one will.
    switch (var) {
    case 1:
        break;
    case 2:
        continue;    //Error!!!
    default:
        break;
}
    while(cond) {
    switch(var) {
        case 1:
            break;
        case 2:
            //jump to next outer loop (e.g. while, for.. iteration)
            continue;
        default:
            break;
    }
}
Your switch operator will work fine without the continue, because it's supposed to do it automatically (more often it's called 'falling-through'). But breaks are essential, if the 'falling-through' behavior is not desirable.

0 comments:

Post a Comment