Switch statement is used to select one of many code blocks to be executed, based on the value of an expression. It is better then if else constion It selects the given list of case values & when a matched value is found ,executes its statements
Note :-
The case value must be char / int type
Case value must be unique
switch (expression) { case constant1: // Code to execute if expression equals constant1 break; case constant2: // Code to execute if expression equals constant2 break; default: // Code to execute if expression does not match any constan }
// C program to print the day using switch #include <stdio.h> // Driver Code int main() { int select; printf("The day with number is "); scanf("%d", &select); switch (select) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Frieday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; default: printf("Invalid Input"); break; } return 0; }
The day with number is 1 Monday
Switch inside brace value is evaluated
the value is checks all case's
if matching value is found, executes that block of statements
if the matching value is not found in any case value, then its go to the default case and executes this related statements
Padma
@2022, Padma Education.
We cannot warrant full correctness of all content, we try to give our best. While using Padma education website, you agree to have read and accepted our terms and Conditions , privacy policy.