Switch statement in c
A switch-statement is a conditional statement through which we pass a variable constant. A variable constant is a value provided by the user which is passed through the switch statement inside the switch block. This value is compared with all the cases inside the switch block until the value is matched with any of the switch cases.
When the match is found, the statement assigned to the particular case is executed.
If the match is not found, then the default case gets executed, and the control goes outside the switch block.
switch(expression or variable) { case constant 1: //statements; break; //optional case constant 2: //statements; break; //optional case constant 3: //statements; break; //optional . . . case constant n://statements; break;//optinal default: exit(0); }
The expression or constant is the variable value provided by the user. The variable can be an integer variable or character variable.
constant 1, constant 2, constant 3....constant n are the cases inside the switch block.
We can understand the switch statement better with the help of an example.
#include<stdio.h> int main() { char grade; // clrscr(); printf("\n Enter the grade\n"); scanf("%c",&grade); if(grade>=97 && grade<=122) //if the input grade is in lower case, it is converted into upper case grade=grade-32; switch(grade) { // this switch keyword consist of a body, and grade is passed to the body through switch keyword. case 'O': printf("\n Excellent"); break; case 'A': printf("\n Very good"); break; case 'B': printf("\n Good"); break; case 'C': printf("\n Average"); break; default: exit(0); } return 0; }
2. When the variable passed through switch keyword is a number, then there is no need for writing it in single quotes. For example, Case 1.
3. If you write the integer case in single quotes. For example., case ‘1’ then it will take the ASCII value of 1.
4.There should be always a space between a keyword case and variable. Eg. case ‘A’.
#include<stdio.h> #include<stdlib.h> int main() { int day; printf("Enter the day:"); scanf("%d",&day);
switch(day) { case 1:printf("monday"); break; case 2:printf("Tuesday"); break; case 3:printf("Wednesday"); break; case 4:printf("Thursday"); break; case 5:printf("Friday"); break; case 6:printf("Saturday"); break; case 7:printf("Sunday"); break; default: exit(0); } return 0; }
As written in the code, there are seven cases from 1 to 7, And the default is the last.
If the input is 1, then the statement inside case 1 will be executed.
We have a break statement after every printf statement. Hence, after the printf statement, the control will come out of the switch statement, and the program will end.
If the input is 4, Thursday will be printed, and so on.
When the input is other than 1 to 7 then, the control will go to the default case, and the program will exit.
#include<stdlib.h> header file is used because it contains exit(0); function in its library file.
Some c compilers may support the exit(0); function without these files, some may not support.
Post a Comment
Post a Comment