Open In App

Nested loops in Objective-C

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Nested while loop flowchart

 

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:

nested do-while loop flowchart

 

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: 

nested For loop flowchart

 

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads