codescracker


objective c

Objective-C Decision Making



« Previous Tutorial Next Tutorial »


Decision making structure requires that the program determine the condition(s) to be evaluated by the Objective-C program, along with the statement(s) to be executed, only if the determined condition becomes true, and optionally the other statement(s) to be executed, in case if the condition becomes false.

Note - In Objective-C programming, any non-zero is assumed to be true and any zero or null, is assumed to be false value.

There are the following types of decision making statement in Objective-C:

Objective-C if statement

The if statement in Objective-C consists of a boolean expression followed by the statement(s). Here is the general form to use the if statement in an Objective-C program:

if(boolean-expression)
{
	statement(s);
}

Here, in case if the boolean-expression evaluates to be true, then the block of code inside the if statement will be executed. And in case if the boolean-expression evaluates to be false, then the program control transfers after the if's closing brace or goes to the next code, exiting the if's statement(s).

Example

Below is an example program demonstrating the if statement in Objective-C:

/* Objective-C Decision Making - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
   int num = 10;
   if(num<20)
   {
       /* if the condition becomes true, then print the following */
       NSLog(@"num is less than 20\n");
   }
   /* this will be printed, in both the case. That is,
    * either the condition becomes true or the condition
    * becomes false */
   NSLog(@"Value of num = %d\n", num);
   return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[13573] num is less than 20
2014-10-03 13:18:37.932 demo[13573] Value of num = 10

Here is another example program, also demonstrating the if statement in Objective-C:

/* Objective-C Decision Making - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
   int num = 100;
   if(num<50)
   {
       /* if the condition becomes true, then print the following */
       NSLog(@"num is less than 50\n");
   }
   /* this will be printed, in both the case. That is,
    * either the condition becomes true or the condition
    * becomes false */
   NSLog(@"Value of num = %d\n", num);
   return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[13573] Value of num = 100

As you can see from the above Objective-C program, the program control transfers next to the if statement body.

Objective-C if-else statement

The if-else statement in Objective-C is same as if statement, except in if-else statement, there is an else statement(s) which is executed when the condition evaluates as false. Otherwise if the condition evaluates as true, then the if statement(s) executed. Here is the general form to use if-else statement in Objective-C:

if(boolean-expression)
{
	statement(s);
}
else
{
	statement(s);
}

Here, if the boolean-expression evaluates to true, then the statement(s) of the if block will be executed, and the program control goes to the next of if-else statement. Otherwise if the boolean-expression evaluates to false, then the statement(s) of the else block will be executed, and the program control goes to the next of the if-else statement. It means that only one statement(s) is executed, either of the if (if the condition is true) or else (if the condition is false).

Example

Here is an example program, illustrating the if-else statement in Objective-C:

/* Objective-C Decision Making - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int num = 50;
	if(num<20)
	{
		NSLog(@"num is less than 20\n");
	}	
	else	
	{
		NSLog(@"num is not less than 20\n");
	}	
	NSLog(@"Value of num = %d\n", num);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[3537] num is not less than 20
2014-10-03 13:18:37.932 demo[3537] Value of num = 50

Objective-C nested if statements

You are free to use one if or if-else statement inside another if or if-else statement(s) in Objective-C. Here is the general form to use nested if statements in Objective-C:

if(boolean-expression-1)
{			
	if(boolean-expression-2)
	{
		if(boolean-expression-3)
		{
			statement(s);
		}
		statement(s);
	}
	else
	{
		statement(s);
	}
}

Example

Here is an example program, illustrates nested if statement in Objective-C:

/* Objective-C Decision Making - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int num1 = 100;
	int num2 = 200;
	if(num1 == 100)
	{
		if(num2 == 200)
		{
			NSLog(@"Value of num1 = 100 and num2 = 200\n");
		}
	}
	NSLog(@"Exact value of num1 = %d\n", num1);
	NSLog(@"Exact value of num2 = %d\n", num2);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[18141] Value of num1 = 100 and num2 = 200
2014-10-03 13:18:37.932 demo[18141] Exact value of num1 = 100
2014-10-03 13:18:37.932 demo[18141] Exact value of num2 = 200

Objective-C switch statements

A switch statement in Objective-C allows a variable to be tested for the equality against some list of values (each values is also called as case). The switch statement is generally and mostly used in a menu-driven program, where there is a option to select a menu to perform particular action. Here is the general form to use switch statement in Objective-C:

switch(expression)
{
	case constant-1:
		statement(s);
		break;
	case constant-2:
		statement(s);
		break;
	case-constant-3:
		statement(s);
		break;
	.
	.
	.
	default:
		statement(s);
		break;
}

Here expression is the expression must have an integral or enumerated type or be of a class type in which the class has a single conversion function to an integral or enumerated type, and case is a keyword, constant-(1, 2, 3, etc) are the case constant values, and default is the keyword used to provide the default statement(s) to be executed if none of the case matched.

Example

Here is an example program demonstrating switch statement in Objective-C:

/* Objective-C Decision Making - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	char grade = 'B';
	switch(grade)
	{
		case 'A' :
			NSLog(@"Excellent. You got maximum.\n");
			break;
		case 'B' :
		case 'C' :
			NSLog(@"Well done.\n");  // both case B and case C have the same value that is Well done.
			break;
		case 'D' :
			NSLog(@"You are just passed\n");
			break;
		case 'F' :
			NSLog(@"Better try again\n");
			break;
		default :
			NSLog(@"Invalid grade\n");
	}
	NSLog(@"Your grade is %c\n", grade);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[17555] Well done.
2014-10-03 13:18:37.932 demo[17555] Your grade is B

You can also use nested switch statements just like a nested if


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test