Objective C switch Statement: Getting Started

objective c switchIf you are looking to create iOS applications using Cocoa or Cocoa Touch APIs, learning Objective C is imperative. Objective C is an object-oriented programming language that adds Smalltalk-style messaging to the C programming language. When you need to control sequence of events or tasks in your application, you should use the control statements offered by Objective C. New to Objective C programming? Take a tutorial at Udemy.com

Control statements in Objective C

We can broadly classify control statements in any language in the following categories:

  • Decision statements: These include the “if-else” statement. This statement is generally used when we have two cases where the decision can toggle. Here is the syntax for “if-else” statement:

if (condition)

{ //statements

}

else

{// statements

}

 So, if the condition stated is true, the line of code in the loop will be executed. But, if the condition is false, the line of code in the else loop will be executed.

  • Iteration statements: These statements include the “for” statement. This statement first initializes a variable, tests the expression for that statement, and then updates the variable, if the expression was true. Here is the syntax for the “for” statement:

For(initialization; condition; increment/decrement)

{ //statements

}

Similarly, other statements that fall under this category are the “while” loop and the “do..while” loop.

  • Selection statements: These statements include the “switch” statements.

In this article, we will learn more about the Objective C switch statement. Consider a situation where there is a condition or expression that needs to be checked for, and then executed for multiple variable combinations. As a straight forward and simplest approach, you would go ahead with using the traditional “if-else” statements. But, as the volume of possible conditions that need to be tested increases, the code starts getting cluttered. The readability of the code for debugging thus reduces. In such situations, where possible conditions are large in number, it is best to go for the switch statement. Objective C inherits the switch statement from the C programming language. Learn about creating apps for iOS devices using Objective C at Udemy.com.

Switch statement syntax

Let us take a look at how the switch statement syntax looks like:

switch (expression) {

case match1:

statements

break;

case match2:

statements

break;

default:

statements

break;

In the above syntax, the expressions can include a condition that returns a value or the value itself. The switch statement operates based on the value returned or held by this expression. Now, as you see there are multiple matches listed as individual cases. For each of these matches, a case statement and match value is required. The match type needs to be same as the value held by the expression. Next comes the break statement. In switch statements, break statement is mandatory. If we do not include this statement, every case result will match up as true, thus executing the corresponding statements. Last, the default statement holds lines of code that are executed if none of the match cases are found true as per the expression.

Example: Switch Statement

To understand the switch statement better, let’s try to understand the following sample code.

#import <Foundation/Foundation.h>

int main ()

{

/* local variable definition */

char value = 'B';

switch(value)

{

case 'A' :

NSLog(@"Your favorite fruit is Mango!\n" );

break;

case 'B' :

NSLog(@"Your favorite fruit is Muskmelon!\n" );

break;

case 'C' :

NSLog(@"Your favorite fruit is Orange\n" );

break;

case 'D' :

NSLog(@"Your favorite fruit is Grapes\n" );

break;

case 'F' :

NSLog(@"Your favorite fruit is Pineapple\n" );

break;

default :

NSLog(@"You don’t like fruits.\n" );

}

return 0;

}

In the above code, we declare a char variable with the value B. This means when the flow reaches the switch statement, it looks for the value passed through the char variable. In this case, it is case B. Thus, the output is seen as follows: Output: Your favorite fruit is Muskmelon

Combining Multiple Cases with Same Condition

In the above example, for each of the case checked against the expression, there existed a statement. But, there might be situations where a bunch of cases might lead to the same line of code being executed. What do we do in such situations? In Objective C, we can combine multiple cases with same statements to reduce the complexity of the code. Here is an example for similar type of coding:

#import <Foundation/Foundation.h>

int main ()

{

/* local variable definition */

char value = '0';

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;

}

In the above code snippet, the cases 0, 1, and 2 need to execute the same line of code if the condition matches. Thus, if the value passes through the switch statement is either 0, 1, or 2, the resultant statement executed will remain the same. Output: zero, one or two

Points to Remember with Switch Statements

Though switch statements are ideal to use in situations where we are building very complex if –else statements, there are certain limitations to using these statements. The switch statement works only with Integral types. This means if you are trying to execute an expression that passes the values of type floating-point numbers, pointers, or Objective-C objects, you will have to use the if-else or other looping methods.

  • You cannot compare an NSString variable with a series of string constants. Because NSString is not an ordinal type, this comparison is not supported.
  • A switch statement must always compare an ordinal type to a constant value. So, if you pass an x value to the switch expression, and in your case statement, compare it with a variable, it would result in an exception.

So, when designing iOS apps that require selection from multiple cases based on user input, you can simply use the switch statement. For example, if you are creating an app where you want to generate a special code based on the letter that is input, you would use the switch statement, where each alphabet will hold a unique number in each of the case statements. Become an expert in Objective C programming. Take a course at Udemy.com