The switch() statement is a selection control structure that allows one of several alternatives to be executed, depending on the value a variable takes. To be more precise, a switch() statement allows a variable to be compared to a list of values. Each value is called a case. If the variable equals a value from the list then the instructions following that particular case are executed.
Compile and run the code below in your own C environment.
#include <stdio.h>
int main(void) {
int a = 10;
int b = 3;
int c;
int okay = 1;
char op = '+';
switch(op) {
case '+':
c = a+b;
break;
case '-':
c = a-b;
break;
case '*':
c = a*b;
break;
case '%':
c = a%b;
break;
case '/':
c = a/b;
break;
default:
printf("Unknown operator.\n");
okay = 0;
c = 0;
}
if (okay) printf("\n%d %c %d = %d\n", a, op, b, c);
return(0);
}
Subsection10.1.1switch() Statement Practice
Activity10.1.
Please complete the program below so that it prints out the correct month, depending on the value of the variable “month”. For example, if month=3, the program should print