Bài giảng Chapter 13 Control Structures

Tài liệu Bài giảng Chapter 13 Control Structures: Chapter 13 Control StructuresControl StructuresConditionalmaking a decision about which code to execute, based on evaluated expressionifif-elseswitchIterationexecuting code multiple times, ending based on evaluated expressionwhilefordo-while2Ifif (condition) action;conditionactionTFCondition is a C expression,which evaluates to TRUE (non-zero) or FALSE (zero).Action is a C statement, which may be simple or compound (a block).3Example If Statementsif (x 3) z = z / 2; else z = z * 2;if (x != 10) { if (y > 3) z = z / 2; else z = z * 2;}is the same as...if (x != 10) { if (y > 3) z = z / 2;} else z = z * 2;is NOT the same as...10Chaining If’s and Else’sif (month == 4 || month == 6 || month == 9 || month == 11) printf(“Month has 30 days.\n”); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) printf(“Month has 31 days.\n”);else if (month == 2) printf(“Month has 28 or 29 days.\n”);else printf(“Don’t know that mon...

ppt43 trang | Chia sẻ: honghanh66 | Lượt xem: 837 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng Chapter 13 Control Structures, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 13 Control StructuresControl StructuresConditionalmaking a decision about which code to execute, based on evaluated expressionifif-elseswitchIterationexecuting code multiple times, ending based on evaluated expressionwhilefordo-while2Ifif (condition) action;conditionactionTFCondition is a C expression,which evaluates to TRUE (non-zero) or FALSE (zero).Action is a C statement, which may be simple or compound (a block).3Example If Statementsif (x 3) z = z / 2; else z = z * 2;if (x != 10) { if (y > 3) z = z / 2; else z = z * 2;}is the same as...if (x != 10) { if (y > 3) z = z / 2;} else z = z * 2;is NOT the same as...10Chaining If’s and Else’sif (month == 4 || month == 6 || month == 9 || month == 11) printf(“Month has 30 days.\n”); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) printf(“Month has 31 days.\n”);else if (month == 2) printf(“Month has 28 or 29 days.\n”);else printf(“Don’t know that month.\n”);11Whilewhile (test) loop_body;testloop_bodyTFExecutes loop body as long as test evaluates to TRUE (non-zero).Note: Test is evaluated before executing loop body.12Generating Code for Whilex = 0; while (x ... ADD R0, R0, #1 ; incr x STR R0, R5, #0 JMP LOOP ; test again DONE ; next statement13Infinite LoopsThe following loop will never terminate: x = 0; while (x ... ; re-init ADD R0, R0, #1 ; incr i STR R0, R5, #0 JMP LOOP ; test again DONE ; next statementThis is the same as the while example!16Example For Loops/* -- what is the output of this loop? -- */for (i = 0; i main() { double pi = 0.0; int numOfTerms, count; printf("Number of terms (must be 1 or larger) : "); scanf("%d", &numOfTerms); for (count=0; count 0divisor =divisor + 1FT31Primes: Using a Flag VariableTo keep track of whether number was divisible, we use a "flag" variable.Set prime = TRUE, assuming that this number is prime.If any divisor divides number evenly, set prime = FALSE.Once it is set to FALSE, it stays FALSE.After all divisors are checked, number is prime if the flag variable is still TRUE.Use macros to help readability.#define TRUE 1 #define FALSE 032Primes: Complete Code#include #define TRUE 1 #define FALSE 0 main () { int num, divisor, prime; /* start with 2 and go up to 100 */ for (num = 2; num main() { char key; /* input character from user */ int match = 0; /* keep track of characters matched */ int count = 0; /* number of substring matches */ /* Read character until newline is typed */ while ((key = getchar()) != '\n') { /* Action depends on number of matches so far */ switch (match) { case 0: /* starting - no matches yet */ if (key == 't') match = 1; break;39Substring: Code (Part 2) case 1: /* 't' has been matched */ if (key == 'h') match = 2; else if (key == 't') match = 1; else match = 0; break; 40Substring: Code (Part 3) case 2: /* 'th' has been matched */ if (key == 'e') { count++; /* increment count */ match = 0; /* go to starting point */ } else if (key == 't') { match = 1; else match = 0; break; } } printf("Number of matches = %d\n", count); }41Break and Continuebreak;used only in switch statement or iteration statementpasses control out of the “smallest” (loop or switch) statement containing it to the statement immediately followingusually used to exit a loop before terminating condition occurs (or to exit switch statement when case is done)continue;used only in iteration statementterminates the execution of the loop body for this iterationloop expression is evaluated to see whether another iteration should be performedif for loop, also executes the re-initializer42ExampleWhat does the following loop do?for (i = 0; i <= 20; i++) { if (i%2 == 0) continue; printf("%d ", i);}What would be an easier way to write this?What happens if break instead of continue?43

Các file đính kèm theo tài liệu này:

  • pptpattpatelch13_2267.ppt
Tài liệu liên quan