In this tutorial, we will discuss switch case statement in C language
The switch statement in C, select one option from multiple options of cases based on a switch expression.
Switch case statements similar to if else if … else ladder but it is not same as if else if else. Switch case statements are used to executes a block of code from multiple options
The following the syntax of the switch case statement to clearly depicts the flow of switch case statements
Syntax
switch(expression) { case constant_1: //block of statements 1 //code to be executed, if the expression is matched to constant_1(case 1) break; case constant_2: //block of statements 2 //code to be executed, if the expression is matched to constant_2(case 2) break; case constant_3: //block of statements 3 //code to be executed, if the expression is matched to constant_3(case 3) break; default: default block; //code to be executed, if the expression doesn't matched to any constant_1(case 1) break; }
In the switch case statement, when a case statement is matched the switch expression, the flow of control of the program to pass the block of code associated with case statements
In this program, when the value of the expression(switch) is equal to the constant_3 program executed and output will be displayed statements of case constrant_3
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int age=30; switch(age){ case 20: printf("age is equal to 20"); break; case 22: printf("age is equal to 22"); break; case 24: printf("age is equal to 24"); break; case 26: printf("age is equal to 26"); break; default: printf("age is not equal to any value"); } getch(); return 0; }
When the above code executed, it produces the following results
age is not equal to any value
In the above program, if the age is defined as 30. the flow of control checks every case to find an alternative case. Then the flow of control fails to find the associated case . the default statement will be displayed as follows
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int month; printf("Enter the month: "); scanf("%d",&month); switch(month){ case 1: printf("This month is January"); break; case 2: printf("This month is February"); break; case 3: printf("This month is march"); break; case 4: printf("This month is april"); break; case 5: printf("This month is May");//associated case break; case 6: printf("This month is June"); break; default: printf("your month is not available in first half months"); break; } getch(); return 0; }
When the above code executed, it produces the following results
Enter the month: 5 This month is May
Points to remember of the switch case statements
The break statement is optional of switch case statement but when we use break statements of switch case statements, we can get clear output with the associated case.
let’s see the example below where I am not using the break statements
#include <stdio.h> #include <stdlib.h> int main() { int num=3;; switch(num){ case 1: printf("January\n"); case 2: printf("February\n"); case 3: printf("March\n"); case 4: printf("April\n"); case 5: printf("May\n"); Default: printf("No match any cases"); } getch(); return 0; }
When the above code executed, it produces the following results
March April May No match any cases
Suggested for you
Nested If statements in C language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…