AppleBuildX Team
Sep 3, 2018 · 3 min read

Loops (For, ForEach, While, do…while)

Loop statements are required to perform the tasks needs to be repeated a specific number of times.

Suppose, if you want to print number from 1 to 10.Without loop you can do it as below.

NSLog(@“1”);

NSLog(@“2”);

NSLog(@“3”);

NSLog(@“4”);

NSLog(@“5”);

NSLog(@“6”);

NSLog(@“7”);

NSLog(@“8”);

NSLog(@“9”);

NSLog(@“10”);

Here you to write the NSLog statement 10 times to print the the numbers from 1 to 10. Here loops can come in picture so you can perform the repeated task without writing same code for multiple time.

FOR LOOP

Syntax of For Loop

for ( initialiser; conditional expression; loop expression )

{

// statements to be executed

}

Using For loop to print the the numbers from 1 to 10

for (int i=1 ; i<=10; i++)

{

NSLog(@”%d”,i);

}

Output :

First time i = 1, So it will print the 1 and increment the i value by +1

Second time i = 2, So it will print the 2 and increment the i value by +1So on…..

When i value reach to 10, it will print the 10 and increment the value of i by 1 so i will be 11 and 11<=10 will not be satisfied and loop will break.

Lets look how to print the all the elements of array using the FOR LOOP

NSMutableArray *arrCities = [NSMutableArray arrayWithObjects:@”Mumbai”,@”Delhi”,@”Ahmedabad”, nil];

for (int i=0; i<arrCities.count; i++)

{

NSLog(@”%@”,[arrCities objectAtIndex:i]);

}

Output :

When loop will be executed first time i = 0 then it will print Mumbai

Then i = 1 ,it will print Delhi

In Last i = 2, it will print Ahmedabad

FOREACH LOOP: They can be used to fetch all the elements of NSArray, NSDictionary (iterates over keys), NSSet, etc One by One.

Lets rewrite the above for loop using foreach loop and fetch the all the elements from the array.

for (NSString *strCityName in arrCities)

{

NSLog(@”%@”,strCityName);

}

ENUMERATION: They can be used to fetch all the elements of NSArray, NSDictionary, NSSet, etc one by one. Check the example below.

NSMutableArray *arrCities = [NSMutableArray arrayWithObjects:@”Mumbai”,@”Delhi”,@”Ahmedabad”, nil];

[arrCities enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSLog(@”City at %lu is %@”,(unsigned long)idx,obj);

}];

Here

obj = object in the array

idx = index of array

stop = its for stopping or breaking the enumeration or loops ( if *stop = YES the loop will terminate )

Output :

Mumbai

Delhi

Ahmedabad

WHILE LOOP : A while loop statement repeatedly executes a statement as long as a given condition is true. The syntax of a while loop is as below.

while(condition)

{

statement(s);

}

int i = 1;

while( i < 10 )

{

NSLog(@”%d”, i);

i++;

}

Output : It will print the numbers from 1 to 9

DO WHILE LOOP : A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute the statement at least one time.

int a = 10;

do

{

NSLog(@”%d”, a);

a = a + 1;

} while( a < 20 );

Output : It will print the numbers from 10 to 19

BREAK : Break statement in loop is useful to terminate the execution of loop statement when our need satisfied means particular condition are met.

For example : In given array of cities when we want to find the Delhi , once found then stop executing the loop. program is for this as below

NSMutableArray *arrCities = [NSMutableArray arrayWithObjects:@”Mumbai”,@”Delhi”,@”Ahmedabad”, nil];

for (int i=0; i<arrCities.count; i++)

{

NSLog(@”%@”,[arrCities objectAtIndex:i]);

NSString *strCity = [arrCities objectAtIndex:i];

if ([strCity isEqualToString:@”Delhi”])

{

break;

}

}

Output Mumbai

CONTINUE : The continue statement in Objective-C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between. The syntax for a continue statement is as below.

NSMutableArray *arrCities = [NSMutableArray arrayWithObjects:@”Mumbai”,@”Delhi”,@”Ahmedabad”, nil];

for (int i=0; i<arrCities.count; i++)

{

NSString *strCity = [arrCities objectAtIndex:i];

if ([strCity isEqualToString:@”Delhi”])

{

continue;

}

NSLog(@”%@”,[arrCities objectAtIndex:i]);

}

Here Delhi will not print in the console.

So Output will be as below

Mumbai

Ahmedabad

Hope you understand about the Loops and its usage.

Thank you for reading :)

Please checkout my youtube channel for video tutorials and subscribe it , share it with your friend.

youtube channel link :https://www.youtube.com/channel/UCWjT8XgKescscxlPbMSVzbQ

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade