The Objective-C switch Statement
Previous | Table of Contents | Next |
Objective-C Flow Control with if and else | Objective-C Looping - The for Statement |
In the above syntax outline expression represents either a value, or an expression which returns a value. This is the value against which the switch operates. Using our example, this would be the integer to be evaluated.
For each possible match a case statement is required, followed by a match value. Each potential match must be of the same type as the governing expression. Following on from the case line are the Objective-C statements that are to be executed in the event of the value matching the case match.
After the statements comes a break statement. This statement breaks out of the switch statement. Failure to provide a break statement results in every case after the matching case evaluating to true (regardless of whether the match is made or not) and the corresponding Objective-C statements executing.
Finally, the default section of the construct defines what should happen if none of the case statements present a match to the expression.
A switch Statement Example
With the above information in mind we may now construct a switch statement which provides the same functionality as our previous and somewhat unwieldy if ... else if ... construct:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int value; printf ("Enter a number between 0 and 5: "); scanf ("%i", &value); switch (value) { case 0: NSLog (@"zero"); break; case 1: NSLog (@"one"); break; case 2: NSLog (@"two"); break; case 3: NSLog (@"three"); break; case 4: NSLog (@"four"); break; case 5: NSLog (@"five"); break; default: NSLog (@"Integer out of range"); break; } [pool drain]; return 0; }
Explaining the Example
When compiled and run, the sample application will prompt for a number between 0 and 5. Once entered, the response is assigned to the integer variable value which in turn is used as the governing variable in the switch statement.
The default option simply displays an out of range message if none of the case statements match the number entered by the user.
Combining case Statements
In the above example, each case had its own set of statements to execute. Sometimes a number of different matches may require the same code to be executed. In this case, it is possible to group case statements together with a common set of statements to be executed when a match for any of the cases is found. For example, we can modify the switch construct in our example so that the same code is executed regardless of whether the user enters 0, 1 or 2:
switch (value) { case 0: case 1: case 2: NSLog (@"zero, one or two"); break; case 3: NSLog (@"three"); break; case 4: NSLog (@"four"); break; case 5: NSLog (@"five"); break; default: NSLog (@"Integer out of range"); break; }
<google>BUY_OBJC_BOTTOM</google>
Previous | Table of Contents | Next |
Objective-C Flow Control with if and else | Objective-C Looping - The for Statement |