In this tutorial you will learn about the C Nested Switch Case Statement and its application with practical example.
C Nested Switch Case Statement
In C programming, when there is a switch case statement inside another switch case statement then it is known as a nested switch case statement.
Table Of Contents−
It is possible to have a switch case statement as a part of another switch case statement.
Nested Switch Case Statement Syntax
Below is the general syntax of nested switch case statement in c programming:
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
switch(exp_1){ case value1: // statements inside outer switch switch(exp_2){ case value1: // statements inside inner switch break; case value2: // statements break; } break; case value2: // statements break; } |
Nested Switch Case Statement Example
Below is a simple example to demonstrate the use of nested switch case statement in c programming:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#include<stdio.h> void main() { int num1, num2, res; char choice; printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); printf("5. Exit\n"); printf("Enter your choice (1-5): "); scanf("%c", &choice); switch(choice) { case '1' : printf("Enter two number: "); scanf("%d%d", &num1, &num2); res = num1 + num2; printf("%d + %d = %d",num1, num2, res); break; case '2' : printf("Enter two number: "); scanf("%d%d", &num1, &num2); res = num1 - num2; printf("%d - %d = %d",num1, num2, res); break; case '3' : printf("Enter two number: "); scanf("%d%d", &num1, &num2); res = num1 * num2; printf("%d * %d = %d",num1, num2, res); break; case '4' : printf("Enter two number: "); scanf("%d%d", &num1, &num2); res = num1/num2; printf("%d / %d = %d",num1, num2, res); break; case '5' : break; default : printf("Wrong choice..!!\n"); } } |
When we run the above C program, will see the following output –
Output:-