switch()

Kapps opantm2+spam at gmail.com
Mon Feb 17 00:06:11 PST 2014


On Sunday, 16 February 2014 at 15:43:31 UTC, Manu wrote:
>
> int difficulty;
> switch(e.note.note)
> {
> case 60: .. case 71:
> difficulty = 0;
> break;
> case 72: .. case 83:
> difficulty = 1;
> break;
> case 84: .. case 95:
> difficulty = 2;
> break;
> case 96: .. case 107:
> difficulty = 3;
> break;
> default:
> difficulty = -1;
> break;
> }

I do agree that the breaks can get rather messy and I don't think 
that the current syntax is perfect. However I also find that in 
most situations where a switch statement is useful, it's not a 
bad idea to have it as a separate method anyways. In this case:

int getDifficulty(int note) {
     switch(note) {
     case 60: .. case 71:
         return 0;
     case 72: .. case 83:
         return 1;
     case 84: .. case 95:
         return 2;
     case 96: .. case 107:
         return 3;
     default:
         return -1;
     }
}

Since it's using a return there's no break required and it's 
arguably a cleaner separation.


More information about the Digitalmars-d mailing list