-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchStatements.c
52 lines (45 loc) · 1.57 KB
/
SwitchStatements.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
int main(){
// switch = A more efficient alternative to using many "else if" statements
// allows a value to be tested for equality against many cases
char grade;
printf("\nEnter a letter grade:");
scanf("%c",&grade);
/* if(grade == 'A'){
printf("Perfect!\n");
}
else if(grade == 'B'){
printf("You did good!\n");
}
else if(grade == 'C'){
printf("You did okay\n");
}
else if(grade == 'D'){
printf("At least it's not an F\n");
}
else if(grade == 'F'){
printf("YOU FAILED\n");
}*/
switch(grade){
case 'A': // If grade is 'A', then execute this line
printf("Perfect!\n");
break;
// "break" is used to break out of the switch and exit,
// if it does not exist here, then if you enter 'A', it will print all the statements together
case 'B': // If grade is 'B', then execute this line
printf("You did good!\n");
break;
case 'C': // If grade is 'C', then execute this line
printf("You did okay!\n");
break;
case 'D': // If grade is 'D', then execute this line
printf("At least it's not an F!\n");
break;
case 'F': // If grade is 'F', then execute this line
printf("YOU FAILED!\n");
break;
default: // If none of the above cases matches, then this line will be executed
printf("Please enter only valid grades!\n");
}
return 0;
}