Nested Switch Statements in Objective-C
Last Updated :
26 Apr, 2025
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.
- Switch Case statement works on a selection control mechanism.
- They are substitutes for long else if ladder which just compares a variable/evaluated condition with several integer values. Most programmers like to change their elseif ladder to switch case statements if the number of if statements is more than 5.
- In a switch statement, the case value can be of char and int type only.
- There can be from 1 to N number of the case statement.
- The values present in the case statement must be unique.
- Each Case statement can have a break statement if required, which means it is optional.
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
- First, The flow of control hit the logical_expression part.
- then the result is compared with each case value,
- if any case value matches the result of logical expression then the flow starts executing the instruction associated with it.
- 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
- again jumps to step 4.
- 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
- break: This keyword is used to jump out from the switch statement, which means it helps to terminate the switch blocks and break out of them.
- default: This keyword is used as the default condition, if all the defined conditions are failed to match then the instructions present after this keyword will definitely execute.
Important points of Switch-Case Statement
- The expression provided in the switch should evaluate as constant values, else it won't be valid.
- The default case statement is optional, if the cases are not met with the expression result then the flow of control will simply jump out from the switch statement.
- You should strictly avoid putting two same case values. Duplicate case values are not allowed.
- The nested switch statement is allowed, You can nest the switch as you like, then all the above statements will be also true for the inner switch statement.
- Case values must be a constant, putting expression will be invalid.
Example:
ObjectiveC
// 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];
}
Like nested if-else, you can nest the switch case statements. A nested switch case means a switch case inside another switch case.
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.
}