Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 5 Repetition Introduction • A section of code that is repeated is called a loop: – because after the last statement in the code is executed, – the program branches, or loops, back to the first statement – and starts another repetition through the code • Each repetition is also called an iteration or pass through the loop A First Book of ANSI C, Fourth Edition 2 Basic Loop Structures • Constructing a repeating section of code requires that four elements be present: – Repetition statement • while statement • for statement • do-while statement – Condition – A statement that initially sets the condition being tested – A statement within the repeating section of code that alters the condition so that it eventually becomes false A First Book of ANSI C, Fourth Edition 3 Pretest and Posttest Loops In a pretest loop , the condition is checked at the beginning of each iteration. In a post-test loop, the condition is checked at the end of each iteration. A First Book of ANSI C, Fourth Edition 4 Counter-Controlled Loop • Counter-controlled loop: – the condition is used to keep track of the number of repetitions – Also known as a fixed-count loop int count=0; while (count < 5) { printf("Hello\n"); count = count + 1; } printf("Finished\n"); Hello Hello Hello Hello Hello Finished Press any key to continue Number of repetitions is known when entering the loop A First Book of ANSI C, Fourth Edition 5 Condition-Controlled Loop • Condition-controlled loop: – the tested condition does not depend on a count being achieved, but rather on a specific value being encountered int mark; printf("Enter a mark: "); scanf("%d", &mark); while (mark<0 || mark>100) { printf("Invalid! Enter a new mark: "); scanf("%d", &mark); } printf("Finished\n"); Enter a mark: -12 Invalid! Enter a new mark: Invalid! Enter a new mark: Invalid! Enter a new mark: Invalid! Enter a new mark: Finished Press any key to continue -65 1000 200 56 Number of repetitions is not known when entering the loop A First Book of ANSI C, Fourth Edition 6 Sentinel-Controlled Loop • Sentinel-controlled loop: – a special input value indicates that there is no more data int num; while (num != -1) { printf("Enter a number: "); scanf("%d", &num); } printf("Finished\n"); Enter a number: 12 Enter a number: 23 Enter a number: 20 Enter a number: -1 Finished Press any key to continue Number of repetitions is not known when entering the loop A First Book of ANSI C, Fourth Edition 7 The while Statement • The general form of the while statement is while (expression) statement; • The transfer of control back to the start of a while statement to reevaluate the expression is known as a program loop • The following is a valid but infinite loop: int count=1; while (count <= 10) printf("%d ",count); A First Book of ANSI C, Fourth Edition 8 The while Statement (cont.) A First Book of ANSI C, Fourth Edition 9 The while Statement (cont.) Output is: 1 2 3 4 5 6 7 8 9 10 Output is: 10 9 8 7 6 5 4 3 2 1 A First Book of ANSI C, Fourth Edition 10 Computing Sums and Averages Using a while Loop A First Book of ANSI C, Fourth Edition 11 Computing Sums and Averages Using a while Loop (cont.) A First Book of ANSI C, Fourth Edition 12 Computing Sums and Averages Using a while Loop (cont.) A First Book of ANSI C, Fourth Edition 13 Computing Sums and Averages Using a while Loop (cont.) Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement Calculating an average A First Book of ANSI C, Fourth Edition 14 Sentinels • A program, such as Program 5.7, can be made much more general by removing the restriction that exactly four numbers are to be entered – The user enters a value for how many numbers will be averaged – You can use a sentinel (a data value used to signal either the start or end of a data series) • The sentinel values must be selected so as not to conflict with legitimate data values A First Book of ANSI C, Fourth Edition 15 Sentinels (continued) A First Book of ANSI C, Fourth Edition 16 Sentinels (continued) • One useful sentinel in C is the named constant EOF (End Of File) – The actual value of EOF is compiler-dependent, but it is always assigned a code that is not used by any other character – EOF is defined in stdio.h A First Book of ANSI C, Fourth Edition 17 Sentinels (continued) A First Book of ANSI C, Fourth Edition 18 The break and continue Statements • A break forces an immediate exit from while, switch, for, and do-while statements only Sample output 1: int count=1; float num; while(count <= 10) { printf("Enter a number: "); scanf("%f", &num); if (num > 76) { printf("You lose!"); break; /* break out of the loop */ } else printf("Keep on truckin!"); count++; } /* break jumps to here */ Enter a number: 1 Keep on truckin!Enter a number: 2 Keep on truckin!Enter a number: 20 Keep on truckin!Enter a number: 100 You lose!Press any key to continue Sample output 2: Enter a Keep on Keep on Keep on Keep on Keep on Keep on Keep on Keep on Keep on Keep on A First Book of ANSI C, Fourth Edition number: 1 truckin!Enter truckin!Enter truckin!Enter truckin!Enter truckin!Enter truckin!Enter truckin!Enter truckin!Enter truckin!Enter truckin!Press a number: 2 a number: 3 a number: 4 a number: 5 a number: 6 a number: 7 a number: 8 a number: 9 a number: 10 any key to continue 19 The break and continue Statements • The continue applies to loops only; when a continue statement is encountered in a loop, the next iteration of the loop begins immediately int count=0; float grade, total=0; while (count < 5) { printf("Enter a grade: "); scanf("%f", &grade); if(grade < 0 || grade > 100) continue; total = total + grade; count = count + 1; } printf("Total is %.2f\n", total); A First Book of ANSI C, Fourth Edition Sample Enter Enter Enter Enter Enter Total Press output 1: a grade: 10 a grade: 20 a grade: 30 a grade: 40 a grade: 50 is 150.00 any key to continue Sample output 2: Enter a grade: 10 Enter a grade: 10 Enter a grade: 30 Enter a grade: -10 Enter a grade: 1000 Enter a grade: 10 Enter a grade: 20 Total is 80.00 Press any key to continue 20 The Null Statement • A semicolon with nothing preceding it is also a valid statement, called the null statement ; • Use the null statement where a statement is syntactically required, but no action is needed • Null statements typically are used either with while or for statements A First Book of ANSI C, Fourth Edition 21 The for Statement • The for statement combines all four elements required to easily produce a loop on the same line for (initializing list; tested expression; altering list) statement; • This statement does not require that any of the items in parentheses be present or that they actually be used for initializing or altering the values in the expression statements – However, the two semicolons must be present • for ( ; count <= 20;) is valid • Omitting tested expression results in infinite loop A First Book of ANSI C, Fourth Edition 22 The for Statement (continued) A First Book of ANSI C, Fourth Edition 23 The for Statement (continued) Output is: 2 4 6 8 10 12 14 16 18 20 A First Book of ANSI C, Fourth Edition 24 The for Statement (continued) A First Book of ANSI C, Fourth Edition 25 The for Statement (continued) A First Book of ANSI C, Fourth Edition 26 The for Statement (continued) Comma-separated list A First Book of ANSI C, Fourth Edition 27 Computing Sums and Averages Using a for Loop A First Book of ANSI C, Fourth Edition 28 Case Studies: Loop Programming Techniques • • • • Technique 1: Selection within a loop Technique 2: Input data validation Technique 3: Interactive loop control Technique 4: Evaluating equations A First Book of ANSI C, Fourth Edition 29 Technique 1: Selection within a Loop A First Book of ANSI C, Fourth Edition 30 Technique 2: Input Data Validation Same code used in lines 6-7! A First Book of ANSI C, Fourth Edition 31 Technique 2: Input Data Validation (cont.) A First Book of ANSI C, Fourth Edition 32 Technique 3: Interactive Loop Control A First Book of ANSI C, Fourth Edition 33 Technique 4: Evaluating Equations A First Book of ANSI C, Fourth Edition 34 Technique 4: Evaluating Equations (cont.) A First Book of ANSI C, Fourth Edition 35 Nested Loops A First Book of ANSI C, Fourth Edition 36 Nested Loops (continued) • Sample run: i j i j i j i j i j is now 1 = 1 j = 2 is now 2 = 1 j = 2 is now 3 = 1 j = 2 is now 4 = 1 j = 2 is now 5 = 1 j = 2 j = 3 j = 4 j = 3 j = 4 j = 3 j = 4 j = 3 j = 4 j = 3 j = 4 A First Book of ANSI C, Fourth Edition 37 Nested Loops (continued) A First Book of ANSI C, Fourth Edition 38 The do-while Statement A First Book of ANSI C, Fourth Edition 39 The do-while Statement (cont.) • The general form of the do statement is do statement; while (expression); • do-while is a posttest loop • One type of application is ideally suited for a posttest loop: input data validation application do { printf("\nEnter an ID number: "); scanf("%f", &idNum); } while (idNum < 1000 || idNum > 1999); A First Book of ANSI C, Fourth Edition 40