From b45e3670cc1272204277140c58d1885682715c0e Mon Sep 17 00:00:00 2001 From: BBaoVanC Date: Tue, 20 Oct 2020 20:54:24 -0500 Subject: [PATCH] letter-grade.c: Change if-else chain to switch statement --- letter-grade.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/letter-grade.c b/letter-grade.c index ae6f968..7123716 100644 --- a/letter-grade.c +++ b/letter-grade.c @@ -7,17 +7,27 @@ int main() { if ((input >= 0) && (input <= 100)) { char letter; - if ((input/10) >= 9) { - letter = 'A'; - } else if ((input/10) >= 8) { - letter = 'B'; - } else if ((input/10) >= 7) { - letter = 'C'; - } else if ((input/10) >= 6) { - letter = 'D'; - } else { - letter = 'F'; + int inputRange = input/10; + + switch(inputRange) { + case 10: + case 9: + letter = 'A'; + break; + case 8: + letter = 'B'; + break; + case 7: + letter = 'C'; + break; + case 6: + letter = 'D'; + break; + case 5: + default: + letter = 'F'; } + printf("Your letter grade is %c\n", letter); } else { printf("ERROR: Input must be no more than 100 and no less than 0\n");