Presentation is loading. Please wait.

Presentation is loading. Please wait.

6. LOOPS. Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter.

Similar presentations


Presentation on theme: "6. LOOPS. Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter."— Presentation transcript:

1 6. LOOPS

2 Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter numbers (0 to terminate): "); scanf("%d", &n); while (n != 0) { sum += n; scanf("%d", &n); } printf("The sum is: %d\n", sum); return 0; }

3 while Statements The while statement has the form while ( expression ) statement The body of a while statement is executed repeatedly as long as the expression is true (has a nonzero value). The expression is tested before each execution of the body. Example: i = 10; while (i > 0) { printf("T minus %d and counting\n", i); --i; }

4 while Statements An alternative: i = 10; while (i) printf("T minus %d and counting\n", i--); Using a nonzero constant as the controlling expression creates an infinite loop: while (1) {... }

5 do Statements The do statement has the form do statement while ( expression ) ; The do statement is the same as the while statement, except that the expression is evaluated after each execution of the loop. Example: i = 10; do { printf("T minus %d and counting\n", i); --i; } while (i > 0);

6 for Statements The for statement has the form for ( expr1 ; expr2 ; expr3 ) statement The following code expresses the meaning of the for statement: expr1; while (expr2) { statement /* exception: */ expr3; /* continue statement */ } /* (later) */

7 for Statements For example, the statement for (i = 10; i > 0; --i) printf("T minus %d and counting\n", i); is equivalent to i = 10; while (i > 0) { printf("T minus %d and counting\n", i); --i; }

8 for Statements Any or all of the three expressions in a for statement may be omitted: i = 10; for (; i > 0; --i) printf("T minus %d and counting\n", i); for (i = 10; i > 0;) printf("T minus %d and counting\n", i--); i = 10; for (; i > 0;) printf("T minus %d and counting\n", i--);

9 for Statements Omitting the middle expression creates an infinite loop. The statement for (;;) {... } is equivalent to while (1) {... }

10 Comma Operator The for statement is normally controlled by three expressions. The comma operator allows us to have more than three: for (sum = 0, i = 1; i <= N; i++) sum += i; In general, a comma expression has the form expr1, expr2 expr1 is first evaluated and its value discarded; the value of expr2 is the value of the entire expression. The comma operator is handy whenever C requires a single expression, but we’d like to have two or more expressions. In practice, it appears primarily in for statements and macros.

11 General Rules for Loops When designing a loop, try to use a for statement instead of a while or do statement, especially if the loop “counts up” or “counts down.” Become familiar with the patterns that most often appear in for statements: Counting up from 0 to N–1: for (i = 0; i < N; i++)... Counting up from 1 to N: for (i = 1; i <= N; i++)... Counting down from N–1 to 0: for (i = N-1; i >= 0; i--)... Counting down from N to 1: for (i = N; i > 0; i--)...

12 General Rules for Loops If a for statement would be awkward, use a while statement instead. Use a do statement instead of a while statement if the controlling condition must be tested after the loop body has been executed.

13 break Statement Can be used to jump out of a while, do, for, or switch statement. Example: sum = 0; while (1) { printf("Enter a number: "); scanf("%d", &i); if (i == 0) break; sum += i; }

14 continue Statement Can appear in while, do, and for statements. Causes the program to skip the rest of the current loop iteration. Example: n = 0; sum = 0; do { scanf("%d", &i); /* read (and sum) */ if (i == 0) continue; /* ten numbers, */ ++n; /* skipping zeroes */ sum += i; } while (n < 10);

15 Null Statement The null statement is just a semicolon: i = 0; ; j = 1; The null statement is primarily good for one thing: writing loops whose bodies are empty. For example, the statement for (div = 2; div < n; div++) if (n % div == 0) break; can be rewritten as a loop with an empty body: for (div = 2; div < n && n % div != 0; div++);

16 Null Statement Warning: Accidentally putting a semicolon after the parentheses in an if, while, or for statement will not be detected by the compiler. if (i < 0); /* error */ printf("Error: i is less than zero\n"); i = 10; while (i > 0); /* error */ { printf("T minus %d and counting\n", i); --i; } for (i = 10; i > 0; --i); /* error */ printf("T minus %d and counting\n", i);


Download ppt "6. LOOPS. Example: Summing a Series of Numbers #include int main(void) { int n, sum = 0; printf("This program sums a series of numbers.\n"); printf("Enter."

Similar presentations


Ads by Google