Top Banner
R epeon and Loop St at ements Chapt er 5 1
38

Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Sep 04, 2018

Download

Documents

dokiet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Repetition and Loop StatementsChapter 5

1

Page 2: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Chapter Objectives

• To understand why repetition is an importantcontrol structure in programming

• To learn about loop control variables and thethree steps needed to control loop repetition

• Compound, increment and decrementoperators

• To learn how to use the C for, while, and do-while statements for writing loops and whento use each statement type

2

Page 3: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Chapter Objectives

• To learn how to accumulate a sum or a product within aloop body

• To learn common loop patterns such as counting loops,sentinel-controlled loops, and flag-controlled loops

• To understand nested loops and how the outer loopcontrol variable and inner loop control variable arechanged in a nested loop

• To learn how to debug programs using a debugger• To learn how to debug programs by adding diagnostic

output statements

3

Page 4: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Conditional Loops

• used when there are programming conditionswhen you will not be able to determine theexact number of loop repetitions before loopexecution begins

4

Page 5: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

5

Page 6: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

6

Page 7: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

7

Page 8: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Loop Design

• Sentinel-Controlled Loops– sentinel value: an end marker that follows the last

item in a list of data• Endfile-Controlled Loops• Infinite Loops on Faulty Data

8

Page 9: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

9

Page 10: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Sentinel Loop Design

• Correct Sentinel Loop1. Initialize sum to zero.2. Get first score.3. while score is not the sentinel

4. Add score to sum.5. Get next score

10

Page 11: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Sentinel Loop Design

• Incorrect Sentinel Loop1. Initialize sum to zero.2. while score is not the sentinel

3. Get score4. Add score to sum.

11

Page 12: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

12

Page 13: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Endfile-Controlled Loop Design

1. Get the first data value and save input status2. while input status does not indicate that end

of file has been reached3. Process data value4. Get next data value and save input status

13

Page 14: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

14

Page 15: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Nested Loops

• Loops may be nested just like other controlstructures

• Nested loops consist of an outer loop with oneor more inner loops

• Each time the outer loop is repeated, theinner loops are reentered, their loop controlexpressions are reevaluated, and all requirediterations are performed

15

Page 16: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

16

Page 17: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

17

Page 18: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

18

Page 19: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

do-while Statement

• For conditions where we know that a loopmust execute at least one time

1. Get a data value2. If data value isn’t in the acceptable range, go

back to step 1.

19

Page 20: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

do-while Syntax

dostatement;

while (loop repetition condition);

/* Find first even number input */do

status = scanf(“%d”, &num);while (status > 0 && (num % 2) != 0);

20

Page 21: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Flag-Controlled Loops for Input Validation

• Sometimes a loop repetition conditionbecomes so complex that placing the fullexpression in its usual spot is awkward

• Simplify the condition by using a flag

• flag• a type int variable used to represent whether or

not a certain event has occurred• 1 (true) and 0 (false)

21

Page 22: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

22

Page 23: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Iterative Approximations

Numerical Analysis

23

Page 24: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

24

Page 25: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

25

Page 26: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

26

Page 27: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Bisection Method for FindingRoots

Case Study

27

Page 28: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

28

Page 29: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

29

Page 30: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

30

Page 31: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

31

Page 32: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

32

Page 33: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

33

Page 34: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

34

Page 35: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Using Debugger Programs

• A debugger program can help you find defectsin a C program

• It lets you execute your program onestatement at a time (single-step execution).

• Use this to trace your program’s execution andobserve the effect of each C statement onvariables you select.

• Separate your program into segments bysetting breakpoints.

35

Page 36: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Debugging without a Debugger

• Insert extra diagnostic calls to printf thatdisplay intermediate results at critical points inyour program.

36

Page 37: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Off-by-One Loop Errors

• A fairly common logic error in programs withloops is a loop that executes on more time orone less time than required.

• If a sentinel-controlled loop performs an extrarepetition, it may erroneously process thesentinel value along with the regular data.

• loop boundaries– initial and final values of the loop control variable

37

Page 38: Repetion and Lop Statements - Temple Universitytuf80213/courses/temple/cis1057/slides/... · Repetition and Loop Statements Chapter 5 1. Chapter Objectives • To understand why repetition

Wrap Up

• Use a loop to repeat steps in a program• Frequently occuring loops– counter-controlled loop– sentinel-controlled loop

• Other useful loops– endfile-controlled loop– input validation loop– general conditional loop

38