It's better to use if statements for this because switch cases can only have one value each which means you would have to list all the values in the range if you use a switch.
Sorry, That way of using the case is for ST, I got confused, in C/C++ if you want to use a kind of range you would need to define every case, for example
switch(score)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// and so on until 59
grade = 'F';break ;
// then do the same for 60-69
here is an example
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
}
If you don't type a break; after a case and you state another case, both cases will do the same action, as you can see in case 'B' and case 'C', both will output "Well done" because there is no break after case 'B'.
So the best option is using the if statement as some users told you before
the difference between using if and case is that in if each 'if' will be evaluated one by one, in the case, all the cases are evaluated at the same time, I mean, if you are using "if else if" your code first will evaluate if the first condition is true, then the second, third, and so on, if you use case it will execute the corresponding line so the "switch" statement is faster than "if else if"
the difference between using if and case is that in if each 'if' will be evaluated one by one, in the case, all the cases are evaluated at the same time
Please explain. The switch has a haphazard list of constants that it has to compare the integral value against. These are obviously simpler tests for equality than what the if-clauses might contain. However, the if..else if does not need to evaluate any further than to the first true.
The use of score/10 maps range 60-69 into value 6 due to the nature of integer division.