41
Computer Science: A Structured Programming Approach Using C 1 Objectives To understand basic loop concepts: pretest loops and post-test loops loop initialization and updating event and counter controlled loops To understand and be able to select the best loop construct for a given problem. To write programs that use the while, for, or do ... while statements. To understand the basic concepts and usage of recursion algorithms. To understand and be able to determine the efficiency of an algorithm through an analysis of its looping constructs. Chapter 6 Chapter 6 Repetition Repetition

Objectives

Embed Size (px)

DESCRIPTION

Chapter 6 Repetition. Objectives. ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop initialization and updating ■ event and counter controlled loops - PowerPoint PPT Presentation

Citation preview

Computer Science: A Structured Programming Approach Using C 1

Objectives ❏ To understand basic loop concepts:

■ pretest loops and post-test loops ■ loop initialization and updating

■ event and counter controlled loops ❏ To understand and be able to select the best loop construct for a given problem. ❏ To write programs that use the while, for, or do ... while statements. ❏ To understand the basic concepts and usage of recursion algorithms. ❏ To understand and be able to determine the efficiency of an algorithm through

an analysis of its looping constructs.

Chapter 6Chapter 6 RepetitionRepetition

Computer Science: A Structured Programming Approach Using C 2

6-1 Concept of a loop

The real power of computers is in their ability to The real power of computers is in their ability to repeat an operation or a series of operations many repeat an operation or a series of operations many times. This repetition, called looping, is one of thetimes. This repetition, called looping, is one of thebasic structured programming concepts.basic structured programming concepts.

Each loop must have an expression that determines if Each loop must have an expression that determines if the loop is done. If it is not done, the loop repeats one the loop is done. If it is not done, the loop repeats one more time; if it is done, the loop terminates. more time; if it is done, the loop terminates.

Computer Science: A Structured Programming Approach Using C 3

FIGURE 6-1 Concept of a Loop

Computer Science: A Structured Programming Approach Using C 4

6-2 Pretest and Post-test Loops

We need to test for the end of a loop, but where should We need to test for the end of a loop, but where should we check it—before or after each iteration? We can we check it—before or after each iteration? We can have either a pre- or a post-test terminating condition. have either a pre- or a post-test terminating condition.

In a pretest loop , the condition is checked at the In a pretest loop , the condition is checked at the beginning of each iteration. beginning of each iteration.

In a post-test loop, the condition is checked at the end In a post-test loop, the condition is checked at the end of each iteration. of each iteration.

Computer Science: A Structured Programming Approach Using C 5

Pretest LoopIn each iteration, the control expression is tested first. If it is true, the loop continues; otherwise, the loop is terminated.

Post-test LoopIn each iteration, the loop action(s) are executed. Then the control expression is tested. If it is true, a new iteration is

started; otherwise, the loop terminates.

NoteNote

Computer Science: A Structured Programming Approach Using C 6

FIGURE 6-2 Pretest and Post-test Loops

Computer Science: A Structured Programming Approach Using C 7

FIGURE 6-3 Two Different Strategies for Doing Exercises

Computer Science: A Structured Programming Approach Using C 8

FIGURE 6-4 Minimum Number of Iterations in Two Loops

Computer Science: A Structured Programming Approach Using C 9

6-3 Initialization and Updating

In addition to the loop control expression, two other In addition to the loop control expression, two other processes, initialization and updating, are associated processes, initialization and updating, are associated with almost all loops.with almost all loops.

Loop InitializationLoop Update

Topics discussed in this section:Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 10

FIGURE 6-5 Loop Initialization and Updating

Computer Science: A Structured Programming Approach Using C 11

FIGURE 6-6 Initialization and Updating for Exercise

Computer Science: A Structured Programming Approach Using C 12

6-4 Event- and Counter-Controlled Loops

All the possible expressions that can be used in a loop All the possible expressions that can be used in a loop limit test can be summarized into two general categories: limit test can be summarized into two general categories: event-controlled loops and counter-controlled loops.event-controlled loops and counter-controlled loops.

Event-Controlled LoopsCounter-Controlled LoopsLoop Comparison

Topics discussed in this section:Topics discussed in this section:

6-4 Event- and Counter-Control

Computer Science: A Structured Programming Approach Using C 13

FIGURE 6-7 Event-controlled Loop Concept

Computer Science: A Structured Programming Approach Using C 14

FIGURE 6-8 Counter-controlled Loop Concept

Computer Science: A Structured Programming Approach Using C 15

Table 6-1 Loop Comparisons

Computer Science: A Structured Programming Approach Using C 16

6-5 Loops in C

C has three loop statements: the C has three loop statements: the whilewhile, the , the forfor, and the , and the do…whiledo…while. The first two are pretest loops, and the. The first two are pretest loops, and thethe third is a post-test loop. We can use all of themthe third is a post-test loop. We can use all of themfor event-controlled and counter-controlled loops.for event-controlled and counter-controlled loops.

The while LoopThe for LoopThe do…while LoopThe Comma Expression

Topics discussed in this section:Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C 17

FIGURE 6-9 C Loop Constructs

Computer Science: A Structured Programming Approach Using C 18

FIGURE 6-10 The while Statement

Computer Science: A Structured Programming Approach Using C 19

FIGURE 6-11 Compound while Statement

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 6-1 Process-control System Example

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 6-2 A while Loop to Print Numbers

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 6-2 A while Loop to Print Numbers

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 6-3 Adding a List of Numbers

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 6-3 Adding a List of Numbers

Computer Science: A Structured Programming Approach Using C 25

FIGURE 6-12 for Statement

Computer Science: A Structured Programming Approach Using C 26

FIGURE 6-13 Compound for Statement

Computer Science: A Structured Programming Approach Using C 27

A for loop is used when a loop is to be executed a known number of times. We can do the same thing with a while

loop, but the for loop is easier to read andmore natural for counting loops.

NoteNote

Computer Science: A Structured Programming Approach Using C 28

FIGURE 6-14 Comparing for and while Loops

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 6-4 Example of a for Loop

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 6-4 Example of a for Loop

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 6-5 A Simple Nested for Loop

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 6-5 A Simple Nested for Loop

Computer Science: A Structured Programming Approach Using C 33

FIGURE 6-15 do…while Statement

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 6-6 Two Simple Loops

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 6-6 Two Simple Loops

Computer Science: A Structured Programming Approach Using C 36

FIGURE 6-16 Pre- and Post-test Loops

Computer Science: A Structured Programming Approach Using C 37

PROGRAM 6-7 Adding a List with the do…while

Computer Science: A Structured Programming Approach Using C 38

PROGRAM 6-7 Adding a List with the do…while

Computer Science: A Structured Programming Approach Using C 39

FIGURE 6-17 Nested Comma Expression

Computer Science: A Structured Programming Approach Using C 40

PROGRAM 6-8 Comparison of while and do…while

Computer Science: A Structured Programming Approach Using C 41

PROGRAM 6-8 Comparison of while and do…while