In programming, there are scenarios where you need to execute a specific block of code multiple times. Typically, program statements are executed in a linear sequence, where the first statement is followed by the second, and so forth. However, programming languages provide control structures to manage more complex execution flows.
A loop statement allows a specific block of code to be executed repeatedly based on a condition. Below is the general structure of a loop statement, which is common across most programming languages: Types of Loops
Types of Loops
For Loop in Objective-C
The for loop in Objective-C is a control structure used for repeating a set of instructions for a specified number of iterations. Unlike while or do-while loops, the for loop allows variable initialization, condition checking, and variable updates all in a single line.
Syntax:
for (initialisation_of_expression; condition_of_expression; update_expression)
{
// Body of the loop
// Instructions to execute
}Components of a For Loop:
1. Initialization: Used to initialize counters or iterators (e.g., int counter = 1;).
2. Condition: Determines how long the loop will run (e.g., counter <= 10;).
3. Updation: Updates the counter or iterator after each iteration (e.g., counter++;).
Examples:
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int counter;
for (counter = 1; counter <= 10; counter++)
{
NSLog(@"%d. Hello, World!", counter);
}
[myPool drain];
return 0;
}Output:
1. Hello, World!
2. Hello, World!
3. Hello, World!
4. Hello, World!
5. Hello, World!
6. Hello, World!
7. Hello, World!
8. Hello, World!
9. Hello, World!
10. Hello, World!Example 2: Breaking Out of a For Loop
The break statement allows exiting the loop before the condition becomes false.
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int counter;
for (counter = 1; counter <= 10; counter++)
{
NSLog(@"%d. Processing...", counter);
if (counter == 7)
{
NSLog(@"Breaking out of the loop...");
break;
}
}
[myPool drain];
return 0;
}Output:
1. Processing...
2. Processing...
3. Processing...
4. Processing...
5. Processing...
6. Processing...
7. Processing...
Breaking out of the loop...Example 3: Nested For Loops
This program demonstrates how nested loops work.
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i, j;
for (i = 1; i <= 3; i++)
{
NSLog(@"Outer loop iteration: %d", i);
for (j = 1; j <= 5; j++)
{
NSLog(@" Inner loop iteration: %d", j);
}
}
[myPool drain];
return 0;
}Output:
Outer loop iteration: 1
Inner loop iteration: 1
Inner loop iteration: 2
Inner loop iteration: 3
Inner loop iteration: 4
Inner loop iteration: 5
Outer loop iteration: 2
Inner loop iteration: 1
Inner loop iteration: 2
Inner loop iteration: 3
Inner loop iteration: 4
Inner loop iteration: 5
Outer loop iteration: 3
Inner loop iteration: 1
Inner loop iteration: 2
Inner loop iteration: 3
Inner loop iteration: 4
Inner loop iteration: 5Example 4: For Loop with Multiple Variables
This example shows a for loop that initializes and updates two variables simultaneously.
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i, j;
for (i = 1, j = 10; i <= 10 && j >= 1; i++, j--)
{
NSLog(@"i = %d and j = %d", i, j);
}
[myPool drain];
return 0;
}Output:
i = 1 and j = 10
i = 2 and j = 9
i = 3 and j = 8
i = 4 and j = 7
i = 5 and j = 6
i = 6 and j = 5
i = 7 and j = 4
i = 8 and j = 3
i = 9 and j = 2
i = 10 and j = 1Do-While Loop in Objective-C
Just like other programming languages, Objective-C supports the do..while loop. A do..while loop is also known as an inverted while loop because, in a while loop, the condition is checked before executing the loop body. If the condition is false, the loop does not execute. However, in a do..while loop, the condition is evaluated at the end of the loop body, ensuring the body executes at least once, regardless of the test condition.
Syntax:
do {
// Loop Body
// Update Statement
} while(expression);Parts of a Do-While Loop:
1. Loop Body: Contains the statements or logic to be executed repeatedly.
2. Update Statement: Modifies the loop control variable (e.g., incrementing or decrementing).
3. Expression: A condition evaluated after the loop body. If true, the loop continues; if false, the loop terminates.
How the Do-While Loop Works:
1. The loop body executes once when the do block is encountered.
2. Statements in the loop body run.
3. The update statement executes.
4. The test condition is evaluated. If true, the control flow returns to Step 2.If false, the loop ends, and control exits the loop.
Example:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int num = 8;
// Do-While Loop
do {
// Loop Body
NSLog(@"Loop is executing.");
// Update Statement
num++;
} while (num < 10);
[pool drain];
return 0;
}Output:
Loop is executing.
Loop is executing.Example 2:
Objective-C program to print the multiplication table of 16:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int num = 1;
int res;
// Do-While Loop
do {
// Loop Body
res = 16 * num;
NSLog(@"16 * %d = %d", num, res);
// Update Statement
num++;
} while (num <= 16);
[pool drain];
return 0;
}Output:
16 * 1 = 16
16 * 2 = 32
16 * 3 = 48
16 * 4 = 64
16 * 5 = 80
16 * 6 = 96
16 * 7 = 112
16 * 8 = 128
16 * 9 = 144
16 * 10 = 160
16 * 11 = 176
16 * 12 = 192
16 * 13 = 208
16 * 14 = 224
16 * 15 = 240
16 * 16 = 256While Loop in Objective-C
In Objective-C, the while loop allows for repeating a statement or a group of statements as long as the specified condition evaluates to true. Unlike other loops, the while loop checks its condition before executing the loop body. This makes it ideal for situations where the number of iterations is not known beforehand. If the condition is true, the loop executes; if the condition is false, the loop exits.
while(condition) {
// Body
}How the While Loop Works:
1. The program encounters the while loop.
2. The condition is evaluated. If the condition is true, control enters the loop body. If the condition is false, the loop exits.
3. After executing the loop body, any necessary updates occur.
4. Control returns to the condition check. Steps 2–3 repeat as long as the condition remains true.
5. When the condition becomes false, the loop ends, and control moves to the next part of the program.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int num = 8;
// While loop execution
while(num < 10) {
NSLog(@"Loop is running.");
num++;
}
[pool drain];
return 0;
}Output:
Loop is running.
Loop is running.Explanation:
1. The variable num is initialized with 8.
2. The while loop checks the condition (num < 10). For num = 8, the condition is true, so the loop body executes, printing “Loop is running,” and num is incremented to 9.. For num = 9, the condition is still true, and the loop executes again.
3. When num = 10, the condition becomes false, and the loop ends.
Example:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int num = 1;
int res;
// While loop execution
while(num <= 15) {
res = 15 * num;
NSLog(@"15 * %d = %d", num, res);
num++;
}
[pool drain];
return 0;
}Output:
15 * 1 = 15
15 * 2 = 30
15 * 3 = 45
15 * 4 = 60
15 * 5 = 75
15 * 6 = 90
15 * 7 = 105
15 * 8 = 120
15 * 9 = 135
15 * 10 = 150
15 * 11 = 165
15 * 12 = 180
15 * 13 = 195
15 * 14 = 210
15 * 15 = 225Nested loops in Objective-C
Objective-C provides three fundamental repetition control structures, commonly known as loops. These loops enable programmers to repeatedly execute a single statement or a block of statements as long as a specified condition is met. Additionally, loops can be embedded within one another, a concept referred to as Nested Loops. Let’s explore them in detail.
Objective-C supports three types of loops that can be nested:
1. Nested While Loop
2. Nested Do-While Loop
3. Nested For Loop
1. Nested While Loop: In Objective-C, a nested while loop occurs when one while loop is placed inside another. For every iteration of the outer loop, the inner loop executes completely, starting from its initial condition and continuing until its termination condition is satisfied.
Syntax:
// Outer while loop
while (condition_expr_1)
{
// Inner while loop
while (condition_expr_2)
{
// Statements executed by the inner loop
statement_1;
// Update the inner loop condition
increment/decrement of condition_expr_2;
}
// Statements executed by the outer loop
statement_3;
// Update the outer loop condition
increment/decrement of condition_expr_1;
}Execution Flow of Nested While Loops
Step 1: The control checks the condition of the outer while loop (condition_expr_1).
Step 2: If condition_expr_1 is true:
- The control enters the body of the outer loop.
- The inner
whileloop condition (condition_expr_2) is checked.
Step 3: The control enters the body of the outer loop. The inner while loop condition (condition_expr_2) is checked.
Step 4: If condition_expr_2 is false, the control exits the inner loop and continues with the statements following it in the outer loop.
Step 5: The outer loop condition (condition_expr_1) is updated and rechecked.
Step 6: If condition_expr_1 is false, the control exits the outer loop and proceeds with the next statements in the program.
Example:
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i = 1;
int j;
// Outer while loop
while (i <= 3)
{
// Message from outer while loop
NSLog(@"%d. Message from outer while loop.", i);
j = 1;
// Inner while loop
while (j <= 5)
{
// Message from inner while loop
NSLog(@" %d. Message from inner while loop.", j);
// Update the inner while loop condition
j++;
}
// Update the outer while loop condition
i++;
}
[myPool drain];
return 0;
}Output:
1. Message from outer while loop.
1. Message from inner while loop.
2. Message from inner while loop.
3. Message from inner while loop.
4. Message from inner while loop.
5. Message from inner while loop.
2. Message from outer while loop.
1. Message from inner while loop.
2. Message from inner while loop.
3. Message from inner while loop.
4. Message from inner while loop.
5. Message from inner while loop.
3. Message from outer while loop.
1. Message from inner while loop.
2. Message from inner while loop.
3. Message from inner while loop.
4. Message from inner while loop.
5. Message from inner while loop.2. Nested Do-While Loop: Similar to a while loop, Objective-C allows you to nest two or more do-while loops within a program. The primary distinction between a while loop and a do-while loop is that a do-while loop executes its body at least once before checking its condition. In the case of nested loops, both the outer and inner do-while loops execute their bodies at least once, following the same basic logic as the while loop.
Syntax
// Outer do-while loop
do
{
// Inner do-while loop
do
{
// Statements executed by the inner do-while loop
statement_1;
// Update the inner do-while loop condition
increment/decrement of condition_expr_2;
} while (condition_expr_2);
// Statements executed by the outer do-while loop
statement_3;
// Update the outer do-while loop condition
increment/decrement of condition_expr_1;
} while (condition_expr_1);Execution Flow of Nested Do-While Loops
Step 1: The control enters the body of the outer do-while loop.
Step 2: The control then enters the body of the inner do-while loop.
Step 3: Execute the body of the inner loop.
Step 4: Update the condition variables of the inner loop.
Step 5: Check the condition of the inner do-while loop: If true it return to Step 2 and if false: Exit the inner loop and move to the next statements in the outer loop.
Step 6: Update the condition variables of the outer loop.
Step 7: Check the condition of the outer do-while loop. If condition is true then it returns to Step 1.If false: Exit the outer loop.
Example:
#import <Foundation/Foundation.h>
int main ()
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
int i = 1;
int j;
// Outer do-while loop
do
{
// Message from outer do-while loop
NSLog(@"%d. Message from outer do-while loop.", i);
j = 1;
// Inner do-while loop
do
{
// Message from inner do-while loop
NSLog(@" %d. Message from inner do-while loop.", j);
// Update the inner loop condition
j++;
} while (j <= 5);
// Update the outer loop condition
i++;
} while (i <= 3);
[myPool drain];
return 0;
}Output:
1. Message from outer do-while loop.
1. Message from inner do-while loop.
2. Message from inner do-while loop.
3. Message from inner do-while loop.
4. Message from inner do-while loop.
5. Message from inner do-while loop.
2. Message from outer do-while loop.
1. Message from inner do-while loop.
2. Message from inner do-while loop.
3. Message from inner do-while loop.
4. Message from inner do-while loop.
5. Message from inner do-while loop.
3. Message from outer do-while loop.
1. Message from inner do-while loop.
2. Message from inner do-while loop.
3. Message from inner do-while loop.
4. Message from inner do-while loop.
5. Message from inner do-while loop.