Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.

Similar presentations


Presentation on theme: "C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound."— Presentation transcript:

1 C Programming Lecture 12

2 The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound statement is itself a statement In C, wherever it is correct to place a statement, you may also place a compound statement.In C, wherever it is correct to place a statement, you may also place a compound statement.

3 Example of a Compound Statement { int a = 1, sum = 0; int a = 1, sum = 0; while (a <= 10) while (a <= 10) { sum = sum + a; sum = sum + a; a = a + 1; a = a + 1; printf(“%d\n”, a); printf(“%d\n”, a); } printf(“%d\n”, sum); printf(“%d\n”, sum);} /* Note there is a compound statement within a compound statement. */

4 The Use of Compound Statements b To group statements into an executable unit. b To achieve the desired flow of control in if, if-else, while, for, do, and switch statements. We are about to study these statements in greater detail.We are about to study these statements in greater detail.

5 The Empty Statement b The empty statement is written as a single semicolon. It is useful where a statement is needed syntactically but no action is required semantically.It is useful where a statement is needed syntactically but no action is required semantically.

6 Delay Loop with Empty Statement int i = 0; int i = 0;...... printf(“Error: reenter data.\n”); printf(“Error: reenter data.\n”); while (++i < 10000) /* delay */ while (++i < 10000) /* delay */ ; system(“clear”); system(“clear”);...... /* while loop keeps message on */ /* while loop keeps message on */ /* the screen momentarily. */ /* the screen momentarily. */

7 The while Statement b General Form of while Statement while (expr) while (expr) statement statement next statement next statement b First expr is evaluated. If it is nonzero (true), then statement is executed, and control is passed back to the beginning of the while loop. b The body of the while loop, namely statement, is executed repeatedly until expr is zero (false). At that point control passes to next statement.

8 Example of while Statement int i = 1, sum = 0; int i = 1, sum = 0;...... while (i <= 10) { while (i <= 10) { sum += i; sum += i; ++i; ++i; } printf(“%d\n”, sum); printf(“%d\n”, sum); Number of times through loop Value of sum first 1 (0 + 1) second 3 (1 + 2) third 6 (3 + 3)

9 The for Statement b General Form of the for Statement for (expr1; expr2; expr3) for (expr1; expr2; expr3) statement statement next statement next statement b First expr1 is evaluated; typically expr1 is used to initialize the loop. b Then expr2 is evaluated; if it is nonzero (true) then statement is executed, expr3 is evaluated, and control passes back to the beginning of the for loop again, except that evaluation of expr1 is skipped. The process continues until expr2 is zero (false), at which point control passes to next statement.

10 Typical Roles of for Loop expr1, expr2, & expr3 b General Form of the for Statement for (expr1; expr2; expr3) for (expr1; expr2; expr3) statement statement next statement next statement b expr1 is used to initialize the loop. b expr2 is a logical expression controlling the iteration. The loop exits when expr2 becomes false. b expr3 typically modifies a variable in expr2 eventually causing expr2 to become false.

11 /* Printing Random Numbers */ #include int main(void) { int i, n; i = 0 printf(“\n%s\n%s”, “Some randomly distributed integers will be printed.”, “How many do you want to see? “); scanf(“%d”, &n); for (i = 0; i < n; ++i) { while (i++ < n) { if (i % 6 == 0)... printf(“\n”);... printf(“%9d”, rand());... } } printf(“\n”); equivalent code return 0; using while }

12 Equivalence of for and while Loops for (expr1; expr2; expr3) statement statementstatement Is Equivalent to: The for loop has the The for loop has the expr1; advantage of keeping while (expr2) { both the control statement (expr2) and the statement (expr2) and the expr3; indexing (expr3) all expr3; indexing (expr3) all } in one place at the next statement top of the loop.

13 Missing Expressions in the for Loop b Any or all of the expressions in a for statement can be missing, but the two semicolons must remain. i = 1; i = 1; sum = 0; sum = 0; for ( ; i <= 10; ) for ( ; i <= 10; ) sum += i++; sum += i++; printf(“%d\n”, sum); printf(“%d\n”, sum); }

14 Embedded for Statements b A for statement can be used as the statement part of an if, if-else, while, or another for statement. b See example “Combinatorics” on page 108. Combinatorics involves combinations and permutations. This program lists all triples of nonnegative numbers that add up to a given number N.Combinatorics involves combinations and permutations. This program lists all triples of nonnegative numbers that add up to a given number N.

15 The Comma Operator b The comma operator has the lowest precedence of any operator in C. It is a binary operator that associates from left to right.It is a binary operator that associates from left to right. b General Form expr1, expr2

16 Value and Type of the Comma Operator b The comma expression as a whole has the value and type of its right operand. b Example: a = 0, b = 1 a = 0, b = 1 If b has been declared as an int, this expression has a value of 1 and a type of int.

17 Use of the comma Operator in a for Statement b You can use the comma operator in a for statement to do multiple initializations and multiple processing of indices. b Examples for (sum = 0, i = 1; i <= n; ++i) sum += i; for (sum = 0, i = 1; i <= n; sum += i, ++i) ;

18 Example of comma Expressions Declarations and Initializations int i, j, k = 3; double x = 3.3; Expression: i = 1, j = 2, ++k + 1 Equiv Expression: ((i = 1), (j = 2)), ((++k) + 1) Value:5 Expression: k != 7, ++ x * 2.0 + 1 Equiv Expression: (k != 7), (((++ x) * 2.0) + 1) Value:9.6

19 Use of Commas in Your Code b Most commas in programs do not represent comma operators. Commas used to separate expressions in argument lists of functions or within initializer lists are not comma operators.Commas used to separate expressions in argument lists of functions or within initializer lists are not comma operators. If a comma operator is to be used in these places, the comma expression in which it occurs must be enclosed within parentheses.If a comma operator is to be used in these places, the comma expression in which it occurs must be enclosed within parentheses.

20 The do Statement b The do statement is a variant of the while statement that tests its condition at the bottom of the loop. General Form of the do Statement dostatement while (expr); next statement

21 Examples of a do Statement do { sum += i; sum += i; scanf(“%d”, &i); scanf(“%d”, &i); } while (i > 0); do { printf(“Input feet: “); printf(“Input feet: “); scanf(“%d”, &feet); scanf(“%d”, &feet); if (feet > 2) if (feet > 2) printf(“\nEnter a 1 or 2:\n\n”); printf(“\nEnter a 1 or 2:\n\n”); } while (feet > 2);

22 The goto Statement b goto causes an unconditional jump to a labeled statement somewhere in the current function. b Form of a labeled statement. label: statement Where label is an identifier.

23 Example of goto and label while (scanf(“%lf”, &x) == 1) { if (x < 0.0) if (x < 0.0) goto negative_alert; goto negative_alert; printf(“%f %f %f”, printf(“%f %f %f”, x, sqrt(x), sqrt(2 * x)); x, sqrt(x), sqrt(2 * x));} negative_alert: if (x < 0.0) printf(“Negative value encountered!\n”); printf(“Negative value encountered!\n”);

24 Same Example Without goto while(scanf(“%lf”, &x) == 1 && x >= 0.0) printf(“%f %f %f\n”, printf(“%f %f %f\n”, x, sqrt(x), sqrt(2 * x)); x, sqrt(x), sqrt(2 * x)); if (x < 0.0) printf(“Negative value encountered!\n); printf(“Negative value encountered!\n); && x >= 0.0 accomplishes what the goto accomplished, namely the printf() will not be executed if a negative number is entered. && x >= 0.0 accomplishes what the goto accomplished, namely the printf() will not be executed if a negative number is entered.

25 When Should goto Be Used? b Almost never (it is never needed). gotos can make a program very difficult to read and understand.gotos can make a program very difficult to read and understand. b In rare instances such as exiting from a deeply nested inner loop to the outermost level, a goto can make a program execute with significantly greater efficiency.

26 The break and continue Statements b The break statement causes an exit from the innermost enclosing loop or switch statement. b The continue statement causes the current iteration of a loop to stop and the next iteration to begin immediately.

27 Example Using a break Statement while (1) { scanf(“%lf”, &x); if (x < 0.0) break; printf(“%f\n”, sqrt(x)); } /* break causes jump to next statement */ /* after the while loop. */

28 Example Using a continue Statement while (cnt < n) { scanf(“%lf”, &x); if (x > -0.01 && x < +0.01) continue; /* disregard small values */ ++cnt; sum += x; /* continue transfers control to */ /* here to immediately begin the */ /* next iteration. */ }


Download ppt "C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound."

Similar presentations


Ads by Google