I'm trying to convert an if statement to a switch statement
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is the original if statement that i'm trying to change:
lastName = input('Enter your lastname: ', 's');
quizGrade = input('Enter quiz grade from 0 - 100: ');
if ( quizGrade <= 59 )
fprintf('%s received a grade of F for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
end
if ( 60 <= quizGrade ) && ( quizGrade <= 69 )
fprintf('%s received a grade of D for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
end
if ( 70 <= quizGrade ) && ( quizGrade <= 79 )
fprintf('%s received a grade of C for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
end
if ( 80 <= quizGrade ) && ( quizGrade <= 89 )
fprintf('%s received a grade of B for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
end
if ( 90 <= quizGrade ) && ( quizGrade < 100 )
fprintf('%s received a grade of B for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
end
When I use the switch case, i don't know what to do next after
Switch (quizGrade)
case (?)
frpintf('%s received a grade of F for the numeric value of %d for Quiz 2.\n', lastName, quizGrade);
I know its not correct to put in every single case from case 0 - case 100. I couldn't do case quiz grade <= 59) either. How do i group the cases so i can get the same answer as the if statements?
0 commentaires
Réponses (3)
the cyclist
le 29 Mar 2016
Modifié(e) : the cyclist
le 29 Mar 2016
On the contrary, switch statements are not useful for comparing ranges of values. Consider, for example, this tip from the documentation for switch:
"A case_expression cannot include relational operators such as < or > for comparison against the switch_expression. To test for inequality, use if, elseif, else statements."
Why do you want to switch to switch? ;-)
It would be useful to convert to a series of if/elseif statements.
if ( 90 <= quizGrade ) && ( quizGrade < 100 )
fprintf('%s received a grade of A for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
elseif ( 80 <= quizGrade )
fprintf('%s received a grade of B for the numeric value of %d for Quiz 2.\n', lastName, quizGrade)
elseif <etc>
end
2 commentaires
the cyclist
le 29 Mar 2016
It is a shame when artificial situations (as school assignments sometimes are) force you to use less-than-optimal coding practice. Walter's answer looks to do what you want. There is a slight wrinkle there, if fractional scores (e.g. 89.5) are possible. You could use rounding (or probably "floor" or "ceil") in the switch statement to get around that.
Stephen's solution is certainly clever (and does not have the fractional score issue), but does not use switch.
Walter Roberson
le 29 Mar 2016
case {60, 61, 62, 63, 64, 65, 66, 67, 68, 69}
grade = 'D';
...
2 commentaires
Stephen23
le 29 Mar 2016
Modifié(e) : Stephen23
le 29 Mar 2016
If you are copying lines of code then you are doing it wrong. One simple find is all that is required:
>> vec = [0,60,70,80,90];
>> str = 'FDCBA';
>> foo = @(m)str(find(m>=vec,1,'last'));
>> bar = @(m,n)fprintf('%s received a grade of %s for the numeric value of %d for Quiz 2.\n', n, foo(m), m);
>> bar(95,'Anna')
Anna received a grade of A for the numeric value of 95 for Quiz 2.
>> bar(85,'Bob')
Bob received a grade of B for the numeric value of 85 for Quiz 2.
>> bar(60,'Chen')
Chen received a grade of D for the numeric value of 60 for Quiz 2.
0 commentaires
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!