# How to simplify these switch statement?

• 08-11-2013, 07:03 PM
dojob
How to simplify these switch statement?
From the following code, we can see the pattern case(i+2): sum[i]++;
My question is how to simplify it to above statement? assuming that there are 10000 case, it would not be a good idea to type case 2 ..... case 10000;

Code:

```        for (int i = 0; i < rolls; i++){             switch(rollDice(n)){                 case 2: sum[0]++; break;                 case 3: sum[1]++; break;                 case 4: sum[2]++; break;                 case 5: sum[3]++; break;                 case 6: sum[4]++; break;                 case 7: sum[5]++; break;                 case 8: sum[6]++; break;                 case 9: sum[7]++; break;                 case 10: sum[8]++; break;                 case 11: sum[9]++; break;                 case 12: sum[10]++; break;             }         }```
• 08-11-2013, 07:31 PM
Norm
Re: How to simplify these switch statement?
Why use a switch statement if the relationship between the case value and the index is a constant?
index = caseValue - 2;
Use an if to detect the case 0 and 1 values
• 08-11-2013, 07:43 PM
dojob
Re: How to simplify these switch statement?
Thanks I have found the way to do it