CHAPTER 11

image

While Loops

While Loops Defined

Like for loops, while loops are used when you want to repeat a similar type of task many times. While loops are used when you want to execute a line of code many times until a condition is met. Here is a while loop that will write to the console window 10 times:

int i = 0; while (i < 10) {    NSLog(@"i = %i", i);    i++;}

This while loop will produce this output:

i = 0i = 1i = 2i = 3i = 4i = 5i = 6i = 7i = 8i = 9

The loop above does the same thing as the for loop from Chapter 10, but note that the specifications for the loop are in different spots. The first thing that you will notice is that you need ...

Get Objective-C Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.