SWITCH CASE IN C WITH EXAMPLES
The Switch statement tests the value of a given expression against a list of case labels, when they get matches, block of statement associated with that case in executed.
The general form of switch case
switch (expression)
{
case label1:
block of statement
break;
case label2:
block of statement
break;
case label3:
block of statement
break;
.
.
.
case labeln:
block of statement
break;
default:
block of statement
break;
}
Note :-
# The Expression and label are integer or characters.
# floats are not allowing in case labels.
# labels much be unique.
# block of statement may be zero or more than one statements.
# Case labels end with colon(:).
# The break is followed by semicolon(;).
# Default case is optional case.
# break is optional, if 2 or more case labels belongs to same statement.
Default case is optional case.
How Case Statement Execute
The expression is made to compared with each case labels, if they matches each others than block of statements related to particular case is executed and than control is transferred to statement that following the switch case.
The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement that following the switch case.
If value of expression is does not match with case label than default statement is execute and than control is transferred to statement that following the switch case.
Examples
Program to accept day number of week and display the corresponding week day
The general form of switch case
switch (expression)
{
case label1:
block of statement
break;
case label2:
block of statement
break;
case label3:
block of statement
break;
.
.
.
case labeln:
block of statement
break;
default:
block of statement
break;
}
Note :-
# The Expression and label are integer or characters.
# floats are not allowing in case labels.
# labels much be unique.
# block of statement may be zero or more than one statements.
# Case labels end with colon(:).
# The break is followed by semicolon(;).
# Default case is optional case.
# break is optional, if 2 or more case labels belongs to same statement.
Default case is optional case.
How Case Statement Execute
The expression is made to compared with each case labels, if they matches each others than block of statements related to particular case is executed and than control is transferred to statement that following the switch case.
The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the statement that following the switch case.
If value of expression is does not match with case label than default statement is execute and than control is transferred to statement that following the switch case.
Examples
Program to accept day number of week and display the corresponding week day
Program to display position of vowel in alphabet
Note :- U is different from u.
0 comments:
Post a Comment