Objective-C Loops

Loops are used to execute a block of codes several number of times. There are the following types of loop in Objective-C:

  • for loop - The for loop is used to execute a sequence of statements multiple times.
  • while loop - The while loop repeats a statement(s) until the given condition returns false. The while loop tests the condition before executing the body of the loop
  • do-while loop - The do-while loop is same as while loop except that it tests the condition at the end of the body of the loop
  • nested loop - You can also use one or more than one loops inside any other loop

The while loop in Objective-C

The while loop in Objective-C is used to executes a target statement until the given condition returns false. Here is the general form to use a while loop in Objective-C:

while(condition)
{
	statement(s);
	incrementation;
}

Here statement(s) may be single statement or block of statements. The condition may be any expression. And the incrementation increments the loop variable every time, before going to check the condition.

Example

Here is an example illustrating the while loop in Objective-C:

/* Objective-C Loops - Example Program */

#import <Foundation/Foundation.h>
int main()
{
	int num = 10;
	while(num<20)
	{
		NSLog(@"num = %d\n", num);
		num++;
	} 
	return 0;
}

When the above code is compile and executed, it would produce the following result:

2014-10-07 22:45:26.827 demo[21195] num = 10
2014-10-07 22:45:26.827 demo[21195] num = 11
2014-10-07 22:45:26.827 demo[21195] num = 12
2014-10-07 22:45:26.827 demo[21195] num = 13
2014-10-07 22:45:26.827 demo[21195] num = 14
2014-10-07 22:45:26.827 demo[21195] num = 15
2014-10-07 22:45:26.828 demo[21195] num = 16
2014-10-07 22:45:26.828 demo[21195] num = 17
2014-10-07 22:45:26.828 demo[21195] num = 18
2014-10-07 22:45:26.828 demo[21195] num = 19

The for loop in Objective-C

The for loop in Objective-C is used to execute a block of code, particular number of times. Here is the general form to use for loop in Objective-C:

for(initialization; condition-checker; incrementation)
{
	statement(s);
}

Here is the explanation of the above code fragment (for loop parameters):

  • The initialization statement, is executed first and at once only. This statement allows you to initialize or declare and initialize the loop control variable like, i=0, or int i=0.
  • The condition-checker statement, is used to check the condition at every times before going to execute the loop body. The condition-checker statement is executed after incrementing the loop variable (except at the first time, since at the first time, the condition-checker statement is executed after executing the initialization statement). If the condition becomes true, then the control goes to the loop body, and if the condition becomes false, then the control exits the loop.
  • The incrementation statement, is used to increment the loop variables every times

Example

Here is an example program, illustrating the for loop in Objective-C:

/* Objective-C Loops - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int num;
	for(num=10; num<20; num=num+1)
	{
		NSLog(@"num = %d\n", num);
	}
	return 0;
}

In the place of "num=num+1", you can also write "++num", or "num++". Here is the sample run of the above Objective-C program:

2014-10-07 22:45:26.827 demo[21195] num = 10
2014-10-07 22:45:26.827 demo[21195] num = 11
2014-10-07 22:45:26.827 demo[21195] num = 12
2014-10-07 22:45:26.827 demo[21195] num = 13
2014-10-07 22:45:26.827 demo[21195] num = 14
2014-10-07 22:45:26.827 demo[21195] num = 15
2014-10-07 22:45:26.828 demo[21195] num = 16
2014-10-07 22:45:26.828 demo[21195] num = 17
2014-10-07 22:45:26.828 demo[21195] num = 18
2014-10-07 22:45:26.828 demo[21195] num = 19

The do-while loop in Objective-C

The do-while loop in Objective-C is same as while loop, except in the do-while, the condition is tested at the bottom of the loop instead of top of the loop (in case of while loop). Here is the general form of the do-while loop in Objective-C:

do
{
	statement(s);
	incrementation;
	
}while(condition);

As you can see, the conditional expression is at the bottom of the loop, so the statement(s) must executes at least once.

Example

Here is an example program demonstrating the do-while loop in Objective-C:

/* Objective-C Loops - Example Program */
		
#import <Foundation/Foundation.h> 
int main()
{
	int num = 10;
	do
	{
		NSLog(@"num = %d\n", num);
		num++;
		
	}while(num<20);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

Above Objective-C Example Output:
2014-10-07 22:45:26.827 demo[21195] num = 10
2014-10-07 22:45:26.827 demo[21195] num = 11
2014-10-07 22:45:26.827 demo[21195] num = 12
2014-10-07 22:45:26.827 demo[21195] num = 13
2014-10-07 22:45:26.827 demo[21195] num = 14
2014-10-07 22:45:26.827 demo[21195] num = 15
2014-10-07 22:45:26.828 demo[21195] num = 16
2014-10-07 22:45:26.828 demo[21195] num = 17
2014-10-07 22:45:26.828 demo[21195] num = 18
2014-10-07 22:45:26.828 demo[21195] num = 19

A nested loop in Objective-C

In Objective-C, you are free to use loop(s) inside loop. Here is the general form to use loop(s) inside a loop:

for(initialization; condition-checker; incrementation)
{
	for(initialization; condition-checker; incrementation)
	{
		while(condition)
		{
			statement(s);
		}
	}
	statement(s);
}

Example

/* Objective-C Loops- Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	int i, j;
	for(i=2; i<50; i++)
	{
		for(j=2; j<=(i/j); j++)
			if(!(i%j))
				break;
			if(j>(i/j))
				NSLog(@"%d is a prime number.\n", i);
	}
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-03 13:18:37.932 demo[1027] 2 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 3 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 5 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 7 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 11 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 13 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 17 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 19 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 23 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 29 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 31 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 37 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 41 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 43 is a prime number.
2014-10-03 13:18:37.932 demo[1027] 47 is a prime number.

Loop Control Statements in Objective-C

Loop control statements change the execution from its normal flow. There are the following loop control statements in Objective-C:

  • continue statement - The continue statement causes the loop to skip the remainder of its loop body and immediately goes to the condition-checker part of the loop
  • break statement - The break statement terminates the loop or switch statement and then transfers the execution to the statement immediately following the loop or switch

The Infinite Loop in Objective-C

The infinite loop means, the loop that runs forever. The loop runs forever only when, the condition never becomes false. If you write the condition-checker code, so that the condition becomes always true, or leave to condition-checker part to makes it always true. Here is an example of the infinite loop in Objective-C:

/* Objective-C Loops - Example Program */
		
#import <Foundation/Foundation.h>
int main()
{
	for(; ;)
	{
		NSLog(@"The loop will run forever.\n");
	}	
	return 0;
}

As already told that when the condition-checker expression is absent, then it is assumed to be always true. If you try to run the above Objective-C program, then the program runs for forever like this:

2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.
2014-10-03 13:18:38.932 demo[1027] The loop will run forever.

The output will continue producing the same output as above.

Objective-C Online Test


« Previous Tutorial Next Tutorial »