Nested loops in Objective-C
Last Updated :
26 Apr, 2025
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 called a Nested loop. Let's discuss them one by one.
Three basic nested loops present which can be nested in Objective-C :
- Nested While Loop.
- Nested Do-While Loop.
- Nested For Loop.
Nested While Loop
A while loop is present inside another while loop is called a nested while loop in objective-C. For each iteration of the outer while loop, the inner while loop will execute from the initial value of its conditions statement to till its termination condition is satisfied.
Syntax:
// outer while loop
while (condition_expr_1 )
{
//inner while loop
while (condition_expr_2 )
{
// this statements will be executes by inner loop.
statement_1
// updation part of inner while loop.
increment/decrement of condition_expr_2;
}
// this statements will be executes by outer loop.
statement_3;
// updation part of outer while loop.
increment/decrement of condition_expr_1;
}
How do Nested while loops execute
Step 1: First, the Control flow will hit the condition part of the outer while loop.
Step 2: Check the defined condition_expr_1.
Step 3: If the given condition evaluates TRUE:
- Sub-Step 1: Flow Enter into the body of the outer while loop.
- Sub-Step 2: Control flow hit the condition part of the inner while loop
- Sub-Step 3: Check the present condition_expr_2.
- Sub-Step 4: if the condition is True :
- flow enters into the body of the inner while loop.
- executes the present statement.
- evaluates the updation part
- then control flow jumps to the Sub-Step 3;
- Sub-Step 5: if the condition is false :
- Sub-Step 6: jumps out from the inner while loop.
- Sub-Step 7: start executing the statements present after the inner while loop.
- Sub-Step 8: updates the condition_expr_1.
- Sub-Step 9: jumps to Step 2.
Step 4: If the condition evaluates False :
Step 5: Control flow will jump to the next line present after the outer while loop.
Flow chart
Following is the control flow diagram of nested while loop:
Example:
ObjectiveC
// Objective-C program to demonstrate nested while loop
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i = 1;
int j;
// outer while loop
while (i <= 3)
{
// Massage from outer while loop
NSLog(@"%d.Message from outer while loop.\n", i);
j = 1;
// Inner while loop
while (j <= 5)
{
// Massage from inner while loop
NSLog(@" %d.Message from inner while loop.\n", j);
// Updation part of inner while loop.
// j will increment by 1 each time .
j = j + 1;
}
// Updation part of outer while loop.
// i will increment by 1 each time .
i++;
}
return 0;
[myPool drain];
}
Output:
2022-11-17 04:42:46.139 jdoodle[23:23] 1.Message from outer while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 1.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 2.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 3.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 4.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 5.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 2.Message from outer while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 1.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 2.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 3.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 4.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 5.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 3.Message from outer while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 1.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 2.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 3.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 4.Message from inner while loop.
2022-11-17 04:42:46.141 jdoodle[23:23] 5.Message from inner while loop.
Nested Do-While Loop
Similar like a while loop, you can nest two or more do-while loops in a program. The key difference between a while loop and a do-while loop is, a do-while loop will execute its body at least one time, and if it is nested, then the outer do-while, as well as the inner do-while, will execute its body one time and the rest of the logic are same as while loop.
Syntax:
// outer do-while loop
do
{
// inner do-while loop
do
{
// this statements will be executes by inner do-while.
statement_1;
// updation part of inner do-while loop.
increment/decrement of condition_expr_2;
} while (condition_expr_2)
// this statements will be executes by outer do-while loop.
statement_3;
/// updation part of outer do-while loop.
increment/decrement of condition_expr_1;
} while (condition_expr_1);
How does the Nested do-while loop execute
Step 1: First, the control flow will hit the body of the outer do-while loop.
Step 2: Then, the control flow hits the body of the inner do-while loop.
Step 3: Execute the body of the inner loop.
Step 4: Update the increment/decrement part of the inner loop.
Step 5: Then come to the condition-checking part of the inner do-while loop.
- if the defined condition evaluates true :
- the loop will again start iterate of the body of the inner do-while loop, which means jumps to Step 2.
- if the condition becomes false.
- jumps out to the next line of the inner do-while loop.
Step 6: Flow, will execute the updation line of the outer do-while loop.
Step 7: Then, come to the condition checking of the outer loop.
Step 8: If, the condition is true :
Step 9: Control flow will jump to Step 1.
Step 10: If, the condition becomes false
Step 11: Flow will jump out from the outer loop.
Flow Chart
Control flow diagram of the nested do-while loop:
Example:
ObjectiveC
// Objective-C program to demonstrate nested do while loop
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i = 1;
int j;
// outer while loop
do
{
// Massage from outer while loop
NSLog(@"%d.Message from outer do-while loop.\n", i);
j = 1;
// inner while loop
do
{
// Massage from inner while loop
NSLog(@" %d.Message from inner do-while loop.\n", j);
// updation part of inner loop.
// j will increment by 1 each time .
j = j + 1;
} while (j <= 5);
// Updation part of outer loop.
// i will increment by 1 each time .
i++;
}while (i <= 3);
return 0;
[myPool drain];
}
Output:
2022-11-17 04:55:21.378 jdoodle[25:25] 1.Message from outer do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 1.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 2.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 3.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 4.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 5.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 2.Message from outer do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 1.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 2.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 3.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 4.Message from inner do-while loop.
2022-11-17 04:55:21.379 jdoodle[25:25] 5.Message from inner do-while loop.
2022-11-17 04:55:21.380 jdoodle[25:25] 3.Message from outer do-while loop.
2022-11-17 04:55:21.380 jdoodle[25:25] 1.Message from inner do-while loop.
2022-11-17 04:55:21.380 jdoodle[25:25] 2.Message from inner do-while loop.
2022-11-17 04:55:21.380 jdoodle[25:25] 3.Message from inner do-while loop.
2022-11-17 04:55:21.380 jdoodle[25:25] 4.Message from inner do-while loop.
2022-11-17 04:55:21.380 jdoodle[25:25] 5.Message from inner do-while loop.
Nested For Loop
The most usable loop is for a loop. Some programmers categorize this loop as a Counter control loop. This loop all the initialization, condition as well as increment/decrement parts in a single line.
Syntax:
// Outer for loop ...
for (initialisation_of_expr_1; condition_of_expr_1; update_expr_1)
{
// inner for loop ...
for (initialisation_of_expr_2; condition_of_expr_2; update_expr_2)
{
// body of the loop.
// instruction given by you.
}
}
How does the Nested For loop execute in Objective-C
Step 1: Control flow hits the initialization part of the outer for loop, and initializes the values.
Step 2: jumps to the condition part.
Step 3: if the condition is true:
- Step 3.1: enter the body and hit the initialization part of the inner for loop and initialize.
- Step 3.2: jumps to the condition part of the inner for loop.
- Step 3.3: if the condition is true:
- enter inside the body of the inner for loop.
- executes all the instruction present inside the body.
- jumps to the updation part, increment/decrement the value as defined.
- then jumps to Step 3.2.
- Step 3.5: if the condition becomes false :
- Step 3.6: jumps out to the next line of the inner for loop.
Step 4: executes the rest of the instruction present inside the body.
Step 5: then jumps to updating part of the outer for loop.
Step 6: flow jumps to Step 3.
Step 7: if the condition of the outer for loop is false.
Step 8: flow will jump out to the next line of the outer for loop.
Flow Chart
The control flow diagram of the nested For loop is the same as the while loop:
Example:
ObjectiveC
// Objective-C program to demonstrate nested for loop
#import <Foundation/Foundation.h>
int main()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i = 1;
int j;
// Outer while loop
for(i = 1; i <= 3; i++)
{
// Massage from outer while loop
NSLog(@"%d.Message from outer for loop.\n", i);
// inner while loop
for(j = 1; j <= 5; j++ )
{
// Massage from inner while loop
NSLog(@" %d.Message from inner for loop.\n", j);
}
}
return 0;
[myPool drain];
}
Output:
2022-11-17 05:07:01.485 jdoodle[25:25] 1.Message from outer for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 1.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 2.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 3.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 4.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 5.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 2.Message from outer for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 1.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 2.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 3.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 4.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 5.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 3.Message from outer for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 1.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 2.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 3.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 4.Message from inner for loop.
2022-11-17 05:07:01.486 jdoodle[25:25] 5.Message from inner for loop.
Difference between While, Do-While, and For loops
There is only a syntactical difference present, the rest of the things are overall the same. The do-while loop is sometimes called an exit control loop whereas the while loop and for loop is called entry control loop. You can use these loops as you want. There is a concept called Time Complexity, which can help you to understand how the loops can affect the duration of execution of your program.
Similar Reads
for loop in Objective-C
Like most programming languages, Objective-C also has a repeating statement called for loop. It is a repetition control statement that allows you to repeatedly execute a single instruction or a block of instructions into a specified number of times. unlike other loops like while-loop or do-while loo
9 min read
While loop in Objective-C
Just like other programming languages Objective-C also supports while loop. While loop is a loop that is used to repeat a statement or a group of statements till the given condition is true. Every time while loop checks the condition before executing its body. Or we can say that we can use a while l
3 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
Inheritance in Objective-C
In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to
4 min read
Interface in Objective-C
Objective-C is an object-oriented programming language that was developed by Apple Inc. for its operating systems. One of the fundamental concepts of object-oriented programming is the ability to define interfaces, which specify the methods and properties that an object must have in order to be cons
7 min read
Do While loop in Objective-C
Just like other programming languages Objective-C also supports do..while loop. do..while loop is also known as an inverted while loop because, in a while loop, the expression is evaluated before executing the code present inside the body of the while loop, if the expression is false, then this loop
3 min read
Nested Loops in Programming
In programming, Nested Loops occur when one loop is placed inside another. These loops are quite useful in day-to-day programming to iterate over complex data structures with more than one dimension, such as a list of lists or a grid. In this article, we will learn about the basics of nested loops a
6 min read
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
NSSet and NSMutableSet in Objective-C
NSSet and NSMutableSet are two classes in Objective-C that are used for representing collections of objects. Both classes provide an ordered set of unique objects, with the difference being that NSSet is immutable, while NSMutableSet is mutable, which means its contents can be changed dynamically. I
3 min read
Nested loops in golang
Nested loops in Go are loops placed within other loops. This structure allows you to iterate over multiple data structures or perform complex operations that require multiple levels of iteration. With each iteration of the outer loop, the inner loop runs from start to finish. Nested loops are common
3 min read