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 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 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.