CHAPTER 12

image

Do While Loops

Do While Loops Defined

Do while loops are used for the same reasons as for loops and while loops. The syntax is different, and do while loops are notable because the code in the block will execute at least once. This is because the ending condition is not evaluated until the end of the loop. Here is how you would code a do while loop to count to 10:

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

This do 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 and the while loop in Chapter 11. The specifications ...

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.