Switch Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

C Switch Statement

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute
multiple operations for the different possibles values of a single variable called switch variable.
Here, We can define various statements in the multiple cases for the different values of a single
variable.

The syntax of switch statement in c language is given below:

1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }

Rules for switch statement in C language


1. The switch expression must be of an integer or character type.
2. The case value must be an integer or character constant.
3. The case value can be used only inside the switch statement.
4. The break statement in switch case is not must. It is optional. If there is no break statement
found in the case, all the cases will be executed present after the matched case. It is known
as fall through the state of C switch statement.

Let's try to understand it by the examples. We are assuming that there are following variables.

1. int x,y,z;
2. char a,b;
3. float f;
Valid Switch Invalid Switch Valid Case Invalid Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case 'x'>'y'; case 1,2,3;

Flowchart of switch statement in C

Functioning of switch case statement


First, the integer expression specified in the switch statement is evaluated. This value is then
matched one by one with the constant values given in the different cases. If a match is found, then
all the statements specified in that case are executed along with the all the cases present after
that case including the default statement. No two cases can have similar values. If the matched
case contains a break statement, then all the cases present after that will be skipped, and the
control comes out of the switch. Otherwise, all the cases following the matched case will be
executed.

How does C switch statement work?


Let's go through the step-by-step process of how the switch statement works in C:

Consider the following switch statement:

C Program:

1. #include <stdio.h>
2.
3. int main() {
4. int num = 2;
5. switch (num) {
6. case 1:
7. printf("Value is 1\n");
8. break;
9. case 2:
10. printf("Value is 2\n");
11. break;
12. case 3:
13. printf("Value is 3\n");
14. break;
15. default:
16. printf("Value is not 1, 2, or 3\n");
17. break;
18. }
19.
20. return 0;
21. }

Output

Value is 2

Step-by-step Process:

1. The switch variable num is evaluated. In this case, num is initialized with the value 2.
2. The evaluated num (2) value is compared with the constants specified in each case label
inside the switch block.
3. The switch statement matches the evaluated value (2) with the constant specified in
the second case (case 2). Since there is a match, the program jumps to the code block
associated with the matching case (case 2).
4. The code block associated with case 2 is executed, which prints "Value is 2" to the
console.
5. The "break" keyword is present in the code block of case 2. As a result, the program
breaks out of the switch statement immediately after executing the code block.
6. The program control continues after the switch statement, and any statements following
the switch statement are executed. In this case, there are no statements after the switch,
so the program terminates.
7. The switch statement evaluated the value of the variable num, found a match with case
2, executed the corresponding code block, and then exited the switch block due to the
presence of the "break" statement.

Example of a switch statement in C

Let us see a simple example of a C language switch statement.

1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. switch(number){
7. case 10:
8. printf("number is equals to 10");
9. break;
10. case 50:
11. printf("number is equal to 50");
12. break;
13. case 100:
14. printf("number is equal to 100");
15. break;
16. default:
17. printf("number is not equal to 10, 50 or 100");
18. }
19. return 0;
20. }

Output

enter a number:4
number is not equal to 10, 50 or 100

enter a number:50
number is equal to 50

Switch case example 2

1. #include <stdio.h>
2. int main()
3. {
4. int x = 10, y = 5;
5. switch(x>y && x+y>0)
6. {
7. case 1:
8. printf("hi");
9. break;
10. case 0:
11. printf("bye");
12. break;
13. default:
14. printf(" Hello bye ");
15. }
16.
17. }

Output

hi
Break and Default keyword in Switch statement
Let us explain and define the "break" and "default" keywords in the context of the switch
statement, along with example code and output.

1. Break Keyword:

The "break" keyword is used within the code block of each case to terminate the switch
statement prematurely. When the program encounters a "break" statement inside a case block,
it immediately exits the switch statement, preventing the execution of subsequent case blocks.
The "break" statement is crucial for avoiding switch statements' "fall-through" behavior.

Example:

Let's take a program to understand the use of the break keyword in C.

1. #include <stdio.h>
2. int main() {
3. int num = 3;
4.
5. switch (num) {
6. case 1:
7. printf("Value is 1\n");
8. break; // Exit the switch statement after executing this case block
9. case 2:
10. printf("Value is 2\n");
11. break; // Exit the switch statement after executing this case block
12. case 3:
13. printf("Value is 3\n");
14. break; // Exit the switch statement after executing this case block
15. default:
16. printf("Value is not 1, 2, or 3\n");
17. break; // Exit the switch statement after executing the default case block
18. }
19.

You might also like