mercredi 21 novembre 2018

fall-through switch for executing a sequence of steps

My program need to execute a sequence of steps from start to end. But based on different input the start point will vary, e.g, some will run from the first step to end, some will run from the 2nd step to the end, some will run from the 3rd to the end, etc.

I just need a simple design, currently I use fall-through switch like this:

switch (step) {
case 1:
     //do the 1st step
     //fall through, so no break here
case 2:
    //do the 2nd step
    //fall through
case 3:
    //do the 3rd step
    //fall through
...
}

It works but the fall through code always makes me uncomfortable. Is there a better and simple way to do it ?

Aucun commentaire:

Enregistrer un commentaire