The switch case statement is used when we have multiple options (choice) and we need to perform a different task for each option. You can do the same thing with the If...else...if ladder.
- How does the switch statesmen work?
- Example 1: switch...case
- Example 2: switch...case
- Example 3: switch...case
- Example 4: switch...case
- Read more about
Read more about:
Syntax:
switch(expression){
case constant1
//code to be executed;
break; //optional
case constant2
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
How does the switch statesmen work?
The expression is evaluated once and compared with the values of each case label.
- If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to constant 2, statements after case constant 2 are executed until break is encountered.
- If there is no match, the default statements are executed.
Flow Chart of switch...case statement:
switch-case statement | welcome2protec.com |
Lets take a simple example to understand the working of switch...case statement in c.
Example 1: switch...case in C:
#include <sdtio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d", &number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 20:
printf("number is equal to 20");
break;
case 30:
printf("number is equal to 30");
break;
default:
printf("number is not equal to 10, 20 or 30");
}
return 0;
}
Output
Output 1:
enter a number:4
number is not equal to 10, 20 or 30
Output 2:
enter a number: 20
number is equal to 20
There is a default statement in switch...case which is optional. If switch expression does not match with any case, default statement are executed by the program. As you can see in output 1.
#include <stdio.h>
int main () {
/* local variable definition */
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}
When the above code is complied and executed, it produces the following result.
Output
Well done
You grade is B
You can use the break statement to end processing of a particular case within the switch statement and to branch to the end of the switch statement. Without break, the program continues to the next case, executing the statements until a break or the end of the statement is reached. In some situations, this continuation may be desirable.
The default statement is executed if no case is equal to the value of switch ( expression ). If the default statement is omitted, and no case match is found, none of the statements in the switch body are executed. There can be at most one default statement. The default statement need not come at the end; it can appear anywhere in the body of the switch statement. A case or default label can only appear inside a switch statement.
In absence of break, all cases below the intended case gets executed one-by-one. Lets take an example to understand it better.
#include <stdio.h>
int main()
{
int x = 0;
switch(x)
{
case 0 : printf("\n Its a zero");
case 1 : printf("\n Its a one");
default : printf("\n Its something else");
}
return 0;
}
Output
INPUT : x = 0
Its a zero
Its a one
Its something else
In above code, if I didn’t use the break statements, then all the cases below the correct case ( case 0) are executed unintentionally. To prevent this ‘break statements’ are used. See below example.
#include <stdio.h>
int main()
{
int x = 0;
switch(key)
{
case 0 : printf("\n Its a zero");
break;
case 1 : printf("\n Its a one");
break;
default : printf("\n Its something else");
break;
}
return 0;
}
Output
INPUT : x = 0
It's a zero
- go to statement
- break statement
- continue statement
- Control statement
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.