C Programming.


switch,case,default statement in C tutorials
-
The switch statement is a multi-way decision-making construct that tests wheather an expression matches one of a number of constant values and branches accordingly.
-
The switch statement is a neater alternative to using else-if statements where multi-way decision making is involved.
-
The expression must be enclosed in parentheses, and the body of the switch statement must be enclosed in braces.
-
Statements can be single or multiple statements with or without braces.
-
The values with case should be constants.The expression is evaluted and the resultant value is compared with each of the values given with case.
-
If any of the values match,the statements following the case, till break, are executed.
-
If none of the case values match the result of expression , then the statements following default,which is optional, are executed.
-
The keyword break is used to delimit the scope of the statemants under a particular case.
Syntax:
switch(expression)
{
case value 1:
statements ;
break ;
case value 2:
statements ;
break ;
default :
statements ;
}
C Program example of Switch case Enter month number and print month in character |
|
|
// Program name : test060.c// example of Switch case Enter month number and print month in character#include <stdio.h>#include <conio.h>void main(){int mno;clrscr();printf( "Enter month number from 1 to 12 : ");scanf("%d",&mno);switch(mno){default:printf("Month number not within 1 to 12 \n"); break;case 1:printf("January\n"); break;case 2:printf( "February\n"); break;case 3:printf("March\n"); break;case 4:printf("April\n"); break;case 5:printf("May\n"); break;case 6:printf("June\n"); break;case 7:printf("July\n"); break;case 8:printf("August\n"); break;case 9:printf("September\n"); break;case 10:printf("October\n"); break;case 11:printf("November\n"); break;case 12:printf("December\n");}getch();} |
Output |
Enter month number from 1 to 12 : 8AugustEnter month number from 1 to 12 : 3MarchEnter month number from 1 to 12 : 15Month number not within 1 to 12 |
C Program to Enter Month in number and Print no of days for that month |
|
|
// Program name : test061.c//WAP to Enter Month in number and Print no of days for that month#include <stdio.h>#include <conio.h>void main(){short mno;printf("Enter month number from 1 to 12: ");scanf("%d",&mno);switch(mno){default:printf("Month no is Not within 1 to 12");break;case 1:case 3:case 5:case 7:case 8:case 10:case 12:printf(" 31 Days in month"); break;case 4:case 6:case 9:case 11:printf("30 Days in month"); break;case 2:int yr;printf("Input year in 4 digit : ");scanf("%d",&yr);if(yr%4==0&&yr%100!=0||yr%400==0)printf("29 Days as Leap Year");elseprintf("28 Days in Month") ;}} |
Output |
Enter month number from 1 to 12 : 331 Days in monthEnter month number from 1 to 12 : 630 Days in monthEnter month number from 1 to 12 : 2Input year in 4 digit : 201229 Days in monthEnter month number from 1 to 12 : 2Input year in 4 digit : 201428 Days in month |
C Program to enter character and print enter character is Vovel or not |
|
|
// Program name : test062.c// WAP to enter character and print enter character is Vovel or not#include <stdio.h>void main(){char ch;printf("Enter a character : ");scanf("%c", &ch);switch(ch){case 'a':case 'A':case 'e':case 'E':case 'i':case 'I':case 'o':case 'O':case 'u':case 'U':printf("%c is a Vowel.\n", ch);break;default:printf("%c is not a Vowel.\n", ch);}} |
Output |
Enter a character : UU is a vowelEnter a character : CC is not a vowel |
-
The program decides if a letter entered as input is a vowel or not.
-
The program starts with defining a character variable called ch. The value of ch is accepted as input
-
The next statement switch(ch) checks that letter entered is vowel or not by checking that letter entered is ‘a’,’A’,’e’,’E’,’i’,’I’,’o’,’O’,’u’,’U’ or not.
-
If alphabet entered is a vowel then statements, will print it is a vowel message, and break statement will transfer control out of switch block.
-
If user enter any character other than vowel, it will execute default message, enter character is not vowel.