Looping in C ------------ Looping is the capability to perform the same sequence of statements over and over again without having to repeatedly type the segment of code over and over again. The "while" Statement --------------------- The "while" statement is one way to repeat a sequence of commands. This command is similar to one in C shell programming. The format differs slightly, though, in C. The format is: while (expression) statement; This is the simplest form and an example is given to illustrate it's usage: a = 5; while (a < 10) a++; This segment is equivalent to: a = 5; a++; a++; a++; a++; a++; In this case, a has an intial value of 5 and is incremented until it reaches 10 (thus failing the test (a < 10)) and then quits. If you wanted to do more than one action inside the loop, this could be specified using curly brackets. The general form then becomes: while (expression) { statement1; statement2; : : statementn; } Example Problem --------------- Add up the first 6 positive integers and output the sum. #include main() { int sum; int n; n = 1; sum = 0; while (n < 7) { sum += n; n++; } printf("The sum is %d\n",sum); } There are two other commands that may be associated with the "while" loop. These are: "break" and "continue". The "break" command causes the program to exit out of the loop. The "continue" statement simply makes the loop jump directly to the next iteration. An example is: i = 0; while (i < 10) { i++; if (i % 2 == 0) continue; printf("The number %d is odd\n",i); } This short segment sets i to 0 and then prints out all odd numbers < 10. The output of this segment would look like this: The number 1 is odd The number 3 is odd The number 5 is odd The number 7 is odd The number 9 is odd The "continue" statement above changed the flow of control to jump back up to the start of the "while" loop. Is the following statement legal: while (b == 10); Yes! It is legal. This statement will run forever if b is 10. The ";" acts as a null statement (a null statement does nothing). The for Statement ----------------- The "for" statement is similar to the "while" statement, but has a different form. The form is: for ( initializing list; expression; altering list) statement; The more general formt is: for ( initializing list; expression; altering list) { statement1; statement2; : : statementn; } An example is: for(i=1; i<10; i++) printf("The number is %d.\n",i); The output is: The number is 1. The number is 2. The number is 3. The number is 4. The number is 5. The number is 6. The number is 7. The number is 8. The number is 9. This is equivalent to: i = 1; while (i<10) printf("The number is %d.\n",i); Here is a sample problem using the "for" loop: Problem: Input a start value and an end value using command line arguments. Make a loop to output the numbers within the specified range. Example: %program6 4 8 4 5 6 7 8 #include #include main(int argc, char **argv) { int start = atoi(argv[1]); int end = atoi(argv[2]); int i; for(i=start; i<=end; i++) printf("%d\n",i); } This program gets the range of values as input from the args. Then it sets up a "for" loop and initializes "i" to the start value. It then tests to see if "i" is less tahn or equal to the end value. If it is, it then prints the number out. Then the increment operator (i++) is executed to increase the value of "i". Is this segment legal: i = start; for( ; i<=end; i++) printf("%d\n".i); Yes. In fact, it is the same as the example above. The ";" acts as a null statement here again. The do Statement ---------------- Both the "while" and "for" statements evaluate an expression at the beginning of the repetition loop. The "do" statement makes its test (comparison) at the end. The simplest form is: do statement; while (expression); An example is: a = 1; do printf("a = %d\n",a); while (a == 1); This segment will run forever since "a" is equal to one at the start and it continues to loop while a is equal to one. The more general form is: do { statement1; statement2; : : statementn; } while (expression); Exercises 5.4 -- Problem #4 --------------------------- Modify the problem#1 program from Exercises 5.3 to use the "do" statement instead of the "for" statement. #include main() { int i; i = 20; do { printf("%d ",i); i -= 4; } while (i >= 0); } The output is: %problem_1 20 16 12 8 4 0