Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [20]
Here’s the dumb way:
#include int main(int argc, const char * argv[]) { printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); printf("Aaron is Cool\n"); return 0; } The smart way is to create a loop. The while loop Rewrite the main() function to look like this: #include int main(int argc, const char * argv[]) { int i = 0; while (i < 12) { printf("%d. Aaron is Cool\n", i); i++; } return 0; } Build and run the program. The conditional (i < 12) is being checked before each execution of the block. The first time it evaluates to false, execution leaps to the code after the block. Notice that the second line of the block increments i. This is important. If i wasn’t incremented, then this loop, as written, would continue forever because the expression would always be true. Here’s a flow-chart of this while loop: Figure 7.1 while loop The for loop some initialization while (some check) { some code some last step } So, the C language has a shortcut: the for loop. In the for loop, the pattern shown above becomes: for (some initialization; some check; some last step) { some code; } Change the program to use a for loop: #include int main(int argc, const char * argv[]) { for (int i = 0; i < 12; i++) { printf("%d. Aaron is Cool\n", i); } return 0; } Figure 7.2 for loop Note that in this simple loop example, you used the loop to dictate the number of times something happens. More commonly, however, loops are used to iterate through a collection of items, such as a list of names. For instance, I could modify this program to use a loop in conjunction with a list of friends’ names. Each time through the loop, a different friend would get to be cool. We’ll see more of collections and loops starting in Chapter 15. break #include int main(int argc, const char * argv[]) { int i; for (i = 0; i < 12; i++) { printf("Checking i = %d\n", i); if (i + 90 == i * i) { break; } } printf("The answer is %d.\n", i); return 0; } Build and run the program. You should see Checking i = 0 Checking i = 1 Checking i = 2 Checking i = 3 Checking i = 4 Checking i = 5 Checking i = 6 Checking i = 7 Checking i = 8 Checking i = 9 Checking i = 10 The answer is 10. Notice that when break is called execution skips directly to the end of the code block. Figure 7.3 Breaking out of a loop continue #include int main(int argc, const char * argv[]) { int
The first loop we’ll use is a while loop. The while construct works something like the if construct we discussed in Chapter 4. You give it an expression and a block of code contained by curly braces. In the if construct, if the expression is true, the block of code is run once. In the while construct, the block is run again and again until the expression becomes false.
The while loop is a general looping structure, but C programmers use the same basic pattern a lot:
Sometimes it is necessary to stop the loop’s execution from the inside the loop. For example, let’s say you want to step through the positive integers looking for the number x, where x + 90 = x2. Your plan is to step through the integers 0 through 11 and pop out of the loop when you find the solution. Change the code:
Sometimes you will find yourself in the middle of the code block, and you need to say, “Forget the rest of this run through the code block and start the next run through the code block.” This is done with the continue command. For example, what if you were pretty sure that no multiples of 3 satisfied the equation? How would you avoid wasting precious time checking those?