Nested Switch Case Example




#include<stdio.h>
#include<conio.h>

void main()
{
    int choicech1ch2;
    clrscr();
    
    printf("\n1.Cars \n2.Mobiles");
    printf("\nEnter the value of choice:> ");
    scanf("%d", &choice);
    
    switch(choice)
    {
        case 1:
        {
            printf("\n1.BMW \n2.Porsche \n3.Audi");
            printf("\nEnter the value of Ch1:>\n");
            scanf("%d", &ch1);
            
            switch(ch1)
            {
                case 1:
                {
                    printf("\nBMW");
                    break:
                }
                case 2:
                {
                    printf("\nPorsche");
                    break:
                }
                case 3:
                {
                    printf("\nAudi");
                    break:
                }
                default:
                {
                    printf("Invalid Choice");
                }
            }
            break;
        }

        case 2:
        {
            printf("\n1.iPhone \n2.Samsung \n3.Poco");
            printf("\nEnter the value of Ch1:>\n");
            scanf("%d", &ch2);
            
            switch(ch1)
            {
                case 1:
                {
                    printf("\niPhone");
                    break:
                }
                case 2:
                {
                    printf("\nSamsung");
                    break:
                }
                case 3:
                {
                    printf("\nPoco");
                    break:
                }
                default:
                {
                    printf("Invalid Choice");
                }
            }
            break;
        }

        default
        {
            printf("Invalid Choice");
        }
    }
    getch();
}



Output:
1.Cars
2.Mobiles
Enter the value of choice:>
2
1.iPhone
2.Samsung
3.Poco
Enter the value of choice:>
3
Poco