Decision Making in Objective-C (if, if…else, Nested if)
Last Updated :
24 Apr, 2023
Like, every programming language, Objective-C also has a decision control statement, namely an if-else statement and a switch statement. These statements help programmers to execute a single or a set of instructions based on a defined condition. If the defined condition evaluates as true, then the statement (or the block of statements) attached will execute, and if evaluates as false, the else part will execute, and in the absence of the else part, the next line of code will execute.
if Statement
The defined_condition of the if and else-if statement must be a boolean expression, which means after the evaluation of the condition it must become either true or false. Only 0 is considered false, the rest of the numbers are considered true and you can build as complex logic as you want by using Logical Operators and Relational Operators with the if-else statement.
Here is an example of a complex expression made by using logical as well as relational inside if statement, which basically checks the number between 50 to 100 :
if(number_A >= 50 && number_A <= 100)
{
/// executes these set of instruction.
}
- first, the control flow will evaluate number_A >= 50.
- if the first condition becomes true then the second condition number_A <= 100 also become true
- then the flow will enter into the body.
- if any one of the conditions evaluates false,
- the control flow will directly jump to the next line of the if statement.
You can also combine with logical OR (||), where the body of the if block will execute if one of the conditions evaluates true.
Control Flow Diagram of a single if statement
Syntax:
if( defined_condition )
{
statement_1;
statement_2;
statement_3;
// if the defined_condition become true,
//statement 1,2,3 will execute,
}
// if defined_condition evaluates false,
// compiler will simply ignore those statements.
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[ NSAutoreleasePool alloc] init];
int number = 8;
if ( number < 10)
{
NSLog ( @"Number %d is less then 10" , number);
}
NSLog ( @"Value of number is %d" , number);
return 0;
[myPool drain];
}
|
Output:
Number 8 is less then 10
Value of number is 8
If-Else Statement
The single if statement will execute a block of statements if the condition evaluates true, if false it will do nothing. Now, what if you want to execute a different set of instructions when the if statement evaluates false, here comes the else statement in pictures. So you can use the else statement to execute another block of instruction when if becomes false.
Control Flow Diagram of if-else statement
Syntax:
if( defined_condition )
{
statement_1;
statement_2;
statement_3;
// if the defined_condition become true,
// statement 1,2,3 will execute,
}
else
{
statement_4;
statement_5;
// if the defined_condition become false,
// statement 4,5 will execute,
}
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[ NSAutoreleasePool alloc] init];
int number = 8;
if ( number % 2 == 0)
{
NSLog ( @"---> %d is a Even number.\n" , number);
}
else
{
NSLog ( @"---> %d is a Odd number.\n" , number);
}
return 0;
[myPool drain];
}
|
Output:
---> 8 is a Even number
Nested if-else statement
A nested if-else statement means, an if statement inside another if statement. These nested if statements are used to build complex logic for your program. Control will check the first if condition and enter the associate block (if the condition evaluates true). then again control checks the inner if statement, if condition of the inner if statement is found as true then execute the inner if body else it will come out from the if block to the next line.
Control Flow Diagram of Nested if-else statement
Syntax:
if( defined_condition )
{
if(inner_Condition)
{
/// if defined_Condition and inner_condition
/// both evaluates as true, then and only then
/// statement_1 will be execute.
statement_1;
}
else
{
/// if defined_Condition evaluates as true but
/// inner_condition evaluates as false, then and only then
/// statement_2 will be execute.
statement_2;
}
}
else
{
/// if the defined_condition become false,
///statement3 will execute,
statement_3;
}
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[ NSAutoreleasePool alloc] init];
int num_1 = 8;
int num_2 = 2;
if ( num_1 % 2 == 0)
{
if (num_2 % 2 == 0)
{
NSLog ( @"%d and %d both are Even number.\n" , num_1, num_2);
}
else
{
NSLog ( @"%d is Even number and\n" , num_1);
NSLog ( @"%d is an Odd number.\n" , num_2);
}
}
else
{
NSLog ( @"%d and %d both are Odd number.\n" , num_1, num_2);
}
return 0;
[myPool drain];
}
|
Output:
8 and 2 both are Even number.
if-else-if-else Statement
Now, consider a scenario where you have to check two conditions to execute two separate blocks of instruction as well as a block of instructions for defaults when both conditions (defined by you) fail. In that case, the if-else-if-else statement comes into the picture. Here if the first condition evaluates false then the control will jump to check the second condition present in else-if block, if this evaluates true then the block of instruction associated with it will be executed, and if evaluate false, then the else part will execute. You can define as many else-if blocks as you required.
Control Flow Diagram of if-else-if-else statement
Syntax:
if( defined_condition_A )
{
statement_1;
statement_2;
statement_3;
// if the defined_condition_A become true,
//statement 1,2,3 will execute,
}
else if (defined_condition_B )
{
statement_4;
statement_5;
// if the defined_condition_B become true,
//statement 4,5 will execute,
}
else
{
statement_6;
// if the defined_condition_A as well as
// defined_condition_B both become false,
//statement 6 will execute,
}
Example:
ObjectiveC
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[ NSAutoreleasePool alloc] init];
int number = 10;
if (number > 0)
{
NSLog ( @"---> %d is a Positive number.\n" , number);
}
else if (number < 0)
{
NSLog ( @"---> %d is a Negative number.\n" , number);
}
else
{
NSLog ( @"---> Value of number is %d\n" , number);
}
return 0;
[myPool drain];
}
|
Output:
---> 10 is a Positive number.
Share your thoughts in the comments
Please Login to comment...