If the condition we want to determine is a constant, ie, the number 1 or letter a, then we could use the case switch statement as an alternative to the if else statement. This is usefull in instances like menus. Case Switch can not work with ranges like in our last homework.
The syntax are:Oh fun! Now here is an example.
#include <stdio.h> #include <conio.h> char choice; void main() { clrscr(); printf("What flavor do you like?"); printf("\n\ta) Apple"); printf("\n\tb) Orange"); printf("\n\tc) Vanilla"); printf("\n\td) Mint\n: "); choice=getche(); switch (choice) { case 'a': printf("\nI like apple too."); break; case 'b': printf("\nI like orange too."); break; case 'c': printf("\nI like vanilla too."); break; case 'd': printf("\nI like mint too."); break; default: printf("Huh?"); } getch(); } |