YouTip LogoYouTip

C Nested Switch

C Nested switch Statement

-- Learning is not just about technology, but also about dreams!

C Tutorial

C Language Tutorial C Introduction C Environment Setup C VScode C Program Structure C Basic Syntax C Data Types C Variables C Constants C Storage Classes C Operators C Decision Making C Loops C Functions C Scope Rules C Arrays C enum (Enumeration) C Pointers C Function Pointers and Callback Functions C Strings C Structures C Unions C Bit Fields C typedef C Input & Output C File I/O C Preprocessors C Header Files C Type Casting C Error Handling C Recursion C Variable Arguments C Memory Management C Undefined Behavior C Command Line Arguments C Safe Functions C Sorting Algorithms C Project Structure C Language Examples C Classic 100 Examples C Quiz

C Standard Library

C Standard Library - Reference Manual <a href="#" title="C Standard Library - ">C Standard Library - <assert.h> <a href="#" title="C Standard Library - ">C Standard Library - <ctype.h> <a href="#" title="C Standard Library - ">C Standard Library - <errno.h> <a href="#" title="C Standard Library - ">C Standard Library - <float.h> <a href="#" title="C Standard Library - ">C Standard Library - <limits.h> <a href="#" title="C Standard Library - ">C Standard Library - <locale.h> <a href="#" title="C Standard Library - ">C Standard Library - <math.h> <a href="#" title="C Standard Library - ">C Standard Library - <setjmp.h> <a href="#" title="C Standard Library - ">C Standard Library - <signal.h> <a href="#" title="C Standard Library - ">C Standard Library - <stdarg.h> <a href="#" title="C Standard Library - ">C Standard Library - <stddef.h> <a href="#" title="C Standard Library - ">C Standard Library - <stdio.h> <a href="#" title="C Standard Library - ">C Standard Library - <stdlib.h> <a href="#" title="C Standard Library - ">C Standard Library - <string.h> <a href="#" title="C Standard Library -

C Nested switch Statement

C Decision Making C Decision Making

You can use a switch as part of the statement sequence of an outer switch, which means you can use one switch statement inside another switch statement. Even if the case constants of the inner and outer switch contain common values, there is no conflict.

Syntax

The syntax of a nested switch statement in C:

switch(ch1) {
   case 'A': 
      printf("This A is part of outer switch" );
      switch(ch2) {
         case 'A':
            printf("This A is part of inner switch" );
            break;
         case 'B': /* inner B case code */
      }
      break;
   case 'B': /* outer B case code */
}

Example

#include<stdio.h>

int main ()
{
   /* local variable definition */
   int a = 100;
   int b = 200;
 
   switch(a) {
      case 100: 
         printf("This is part of outer switchn", a );
         switch(b) {
            case 200:
               printf("This is part of inner switchn", a );
         }
   }
   
   printf("Exact value of a is %dn", a );
   printf("Exact value of b is %dn", b );
 
   return 0;
}

When the above code is compiled and executed, it produces the following result:

This is part of outer switch
This is part of inner switch
Exact value of a is 100
Exact value of b is 200

C Decision Making C Decision Making


C Operators

C Loops


1 Note(s)

  1. #include<stdio.h>
    
    int main(void)
    {
        char sex;
        int age;
        printf("Please enter your gender abbreviation! Male(M), Female(F)n");
        scanf_s("%c", &sex);
        switch (sex)
        {
        case 'M':
        case 'm':
            printf("Your gender is "Male", please proceed to the test!n");
            printf("Please enter your years of service!n");
            scanf_s("%2d", &age);
            switch (age)
            {
            case 5:
                printf("Reward: one iPhone!!n");
                break;
            case 10:
                printf("Reward: one car!!n");
                break;
            case 15:
                printf("Reward: one small house!!n");
                break;
            default:
                printf("Sorry, you do not meet the reward conditions or have exceeded the years of service!!n");
                break;
            }
            break;
        case 'F':
        case 'f':
            printf("Your gender is "Female", please proceed to the test!n");
            printf("Please enter your years of service!n");
            scanf_s("%2d", &age);
            switch (age)
            {
            case 5:
                printf("Reward: one iPhone!n");
                break;
            case 10:
                printf("Reward: one set of brand-name cosmetics!n");
                break;
            case 15:
                printf("Reward: one Hermès bag!n");
                break;
            default:
                printf("Sorry, you do not meet the reward conditions or have exceeded the years of service!!n");
                break;
            }
            break;
        }
        return 0;
    }

    Switch statement nesting, testing a years-of-service reward mini program!

← Php Remove SapiPhp Removed Extensions β†’