Decision Making in Objective-C (if, if…else, Nested if)
Last Updated :
26 Apr, 2025
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.
Similar Reads
Decision Making in R Programming - if, if-else, if-else-if ladder, nested if-else, and switch
Decision making is about deciding the order of execution of statements based on certain conditions. In decision making programmer needs to provide some condition which is evaluated by the program, along with it there also provided some statements which are executed if the condition is true and optio
5 min read
Nested loops in Objective-C
Like most programming languages, Objective-C also gives three basic repetition control statements, namely loops, which help programmers to execute a single or block of statements repetitively till the given condition is satisfied. Furthermore, these loops can be used one inside another which is call
10 min read
Decision Making in C++
Decision-making is the process to make a decision about which part of the code should be executed or not based on some condition. Decision-making in C++ involves the usage of conditional statements (also called decision control statements) to execute specific blocks of code primarily based on given
8 min read
If Else If Statement in Programming
If else if statement in programming allows you to check multiple conditions sequentially and execute different blocks of code based on those conditions. It's a way to handle various cases and make decisions within a program efficiently. Table of Content What is If Else If Statement?Syntax of If Else
4 min read
Swift - Decision Making Statements
Decision-making statements are those statements in which the compiler needs to make decisions on the given conditions. If the given condition is satisfied then the set of statements is executed otherwise some other set of statements are executed. These statements always return a boolean value that i
5 min read
IF-ELSE-IF statement in R
if-else-if ladder in R Programming Language is used to perform decision making. This ladder is used to raise multiple conditions to evaluate the expressions and take an output based on it. This can be used to evaluate expressions based on single or multiple conditions connected by comparison or arit
2 min read
VBA Multiple (Nested) If Statements in Excel
VBA in Excel stands for Visual Basic for Applications, which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. What decision-making reasoning do you often use in your Excel worksheets? In most circums
5 min read
Error Handling in Objective-C
In this article, we will learn about error handling in objective C. There are different methods to handle errors in Objective-C with the help of examples and code. What is Error Handling?Error handling is defined as the process or mechanism in programming languages for identifying, catching, and res
6 min read
Numbers in Objective-C
Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
Decision Making in LISP
Decision making is used to specify the condition to evaluate an expression in LISP. There are 4 types of decision-making statements in LISP. They are ifcondwhencaseif Statement The if is a decision-making statement used to check whether the condition is right or wrong. The if the condition is right,
6 min read