Open In App

Nested Switch Statements in Objective-C

Like most of the popular programming languages, Objective-C also allows using decision control statements, namely Switch-Case statements. This statement has an expression part, and a cases-value part, where each case contains a unique value. After evaluating the logical expression, the result of that will sequentially get compared with each case one by one. if any of the cases matched with the result then the block of instructions present with the match case will get executed.  Now, if the evaluated result is not matched with any present cases, then the default instruction comes into the picture.

Here is the control flow chart of the switch-case statement:



 

Syntax:

switch( logical_expression )



{

   case constant_1 :

      statement_0;

      statement_1;

      statement_2;

      break; 

   case constant_2 :

      statement;

      break; 

   case constant_3 :

      statement;

      break; // this statement is for exiting from switch statement.

   // you can have any number of case statements

   // Optional, this will execute if all the cases are fails …

   default : 

      statement_k;

}

Working of Switch-Case Statement

  1. First, The flow of control hit the logical_expression part.
  2. then the result is compared with each case value,
  3. if any case value matches the result of logical expression then the flow starts executing the instruction associated with it.
  4. if the break statement is present at the following case value, then the control flow will simply jump out of the switch statement. and if the break statement is not present, then the flow will execute the next case value (if present). and 
  5. again jumps to step 4.
  6. If any of the given case values failed to match with the result of the logical_expression then the control flow starts looking for the default statement if present then the associated instruction will be executed, and if not present then the flow will jump out from the switch statement.

Keywords used in Switch-Case Statement

Important points of Switch-Case Statement

Example:




// Objective-C program of swtich case statements
#import <Foundation/Foundation.h>
 
int main()
{
    NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
     
    // vowel and consonant program ..
    char ch = 'a';
      scanf("%c", &ch);
     
    switch(ch)
    {
        // if ch == 'a'
        case 'a':
            NSLog(@"%c is a vowel\n", ch);
            break;
             
        // if ch == 'e'
        case 'e':
            NSLog(@"%c is a vowel\n", ch);
            break;
         
        // if ch == 'i'   
        case 'i':
            NSLog(@"%c is a vowel\n", ch);
            break;
         
        /// if ch == 'o'   
        case 'o':
            NSLog(@"%c is a vowel\n", ch);
            break;
         
        // if ch == 'u'
        case 'u':
            NSLog(@"%c is a vowel\n", ch);
            break;
         
         
        // else for all other characters ...
        default:
            NSLog(@"%c is a consonant\n", ch);
    }
     
      return 0;
      [myPool drain];
}

Output:

 

Nested Switch-Case 

Like nested if-else, you can nest the switch case statements. A nested switch case means a switch case inside another switch case.

Important key points on the Nested switch-case statement

  1. The expression used in a switch statement must have an integral or character type, or be of a class type in which the class has a single conversion function to an integral or character type.
  2. There can be any number of case statements within a switch. Each case is followed by the value to be compared to and after that a colon.
  3. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  4. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  5. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached i.e. all the case statements will get executed as soon as the compiler finds a comparison to be true.
  6. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Syntax:

switch(logical_expression)

{

   case constant_1 :

    // inner switch case 

      switch( logical_expression )

{

    case constant_1 :

      statement_1;

      statement_2;

      break; 

    case constant_2 :

      statement;

      break; 

    default : 

      statement_l;

}

      break; 

   case constant_2 :

      /// inner switch case 

      switch( logical_expression )

{

    case constant_1 :

      statement_1;

      statement_2;

      break; 

     

    case constant_2 :

      statement;

      break; 

    default : 

      statement_p;

}

      break;

      

   case constant_3 :

      statement;

      break; 

      

   case constant_4 :

      statement;

      break; 

   

   default : 

      statement_k;

      /// You can make switch case inside this/defaults section also.

}

Example:




// Objective-C program of nested switch-case statements
#import <Foundation/Foundation.h>
 
int main ()
{
    NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
 
    int a = 1, b = 2;
  
    // Outer switch-case
    switch(a)
    {
        // if a == 1
        case 1:
         
            // Nested switch-case / inner switch-case
            switch (b)
            {
                // if b == 2
                case 2:
                    NSLog(@"Your choice is 2.\n");
                    break;
          
                // if b == 3
                case 3:
                    NSLog(@"Your choice is 3.\n");
                    break;
            }
            break;
      
        // if a == 4
        case 4:
            NSLog(@"Your choice is 4.\n");
            break;
      
        // if a == 5
        case 5:
            NSLog(@"Your choice is 5.\n");
            break;
      
        default:
            NSLog(@"Your choice is other than 1, 2 3, 4, 5.\n");
            break;
    }
      return 0;
      [myPool drain];
}

Output:

 

 


Article Tags :