Skip to main content

switch statements:

A switch is a way to do multiple selection pathways on matching of a values. things like >, < etc. types of checks cannot really be done

//in a deck of playing cards many of the values are numeric 2, 3, ...10, but there 4 that are not numeric, A (1), J(11), Q(12), K(13) (ace, jacks queens, kings).  Given the numeric rank of a card print out the associated value of the card (2-10, A, J, Q, K)

void printFace(int numeric){

if(numeric == 1){
printf("A\n");
}
else if(numeric == 11){
printf("J\n")
}
...

}

The same problem using switch

void printFace(int numeric){
switch(numeric){
case 1:
printf("A\n");
break;
case 11:
printf("J\n");
break;
case 12:
printf("Q\n");
break;
case 13:
printf("K\n");
break;
default:
printf("%d", numeric);
};
}