I have an array of float from wich I calculate the mean and the standard deviation. The user can specify a number of class varying between 2 and 8. The min and max values of each class is calculated by a number of standard deviation above and below the mean.


nbr_classes = 4;
mean = 10;
min_value = 1
max_value = 10
std_deviation = 2

class_1_min = min_value
class_1_max = mean - 2*std_deviation

class_2_min = class_1_max
class_2_max = mean - std_deviation

class_3_min = class_2_max
class_3_max = mean + std_deviation

class_4_min = class_3_max
class_4_max = max_value

I would like to create a function that would do that programatically, knowing that number of classes can be odd


1 Answer 1


I don't know what language you are using. Typically one would use the switch statement.

switch(case): { case exp1: \\statements for case 1\\ break;

case exp2: \\statements for case 2\\ break; ...


You can also set a fail safe case. Some languages do not support switch in which case you can just use a long set of if-elseif statements

  • $\begingroup$ you mean setting one case by number of classes between 2 and 8? Thats interesting, in fact I was scratching my head to do it in a loop for i < nbr_classes.length. By the way its for javascript, so yes switch could be a solution $\endgroup$ Aug 15, 2014 at 19:27
  • $\begingroup$ Yes, you specify case 2: blah blah ; The advantage over if/elseif is improved readability. $\endgroup$
    – Sid
    Aug 15, 2014 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.