switch() can only contain char and int.
break is used to exit from switch statement. switch case can be without default case.
In switch case char variable is always initialized within ”(single quotes). If there is no break statement then the cases following the matched case other than default will get executed. If there is no any match case then default will be executed.
Syntax
switch(expression) { case value1: block1; break; case value2: block2; break; case value3: block3; break; case value4: block4; break; default: defaultblock; break; }
Example is below ::
#include<stdio.h> #include<conio.h> void main() { char grade; clrscr(); printf("Enter your grade:\n"); scanf("%c", &grade); switch(grade) { case 'A': printf("\nExcellent"); break; case 'B': printf("\nKeep it up!"); break; case 'C': printf("\nWell done"); break; case 'D': printf("\nYou passed"); break; case 'F': printf("\nBetter luck next time"); break; default: printf("\nInvalid grade"); } printf("Your grade is %c\n",grade); getch(); }
OutPut
Enter your grade: A Excellent Your grade is A
It’s Done. Keep It Up Guys…
Hi, very nice website, cheers!
——————————————————