Programming in C++ Chapter 6

39
Chapter 6 Looping Dale/Weems

description

Chapter 6-

Transcript of Programming in C++ Chapter 6

Page 1: Programming in C++ Chapter 6

Chapter 6

Looping

Dale/Weems

Page 2: Programming in C++ Chapter 6

2

Chapter 6 Topics ●  while Statement Syntax ●  Using the End-of-File Condition to Control

Input Data ●  Using a while loop for Summing or Counting ●  for loop ●  Nested Loops ●  Loop Testing and Debugging

Page 3: Programming in C++ Chapter 6

3

What is a loop?

A loop is a repetition control structure that causes a single statement or block to be executed repeatedly

Loops

Page 4: Programming in C++ Chapter 6

4

Two Types of Loops

Count controlled loops Repeat a statement or block a specified number of times

Event-controlled loops

Repeat a statement or block until a condition within the loop body changes that causes the repetition to stop

Page 5: Programming in C++ Chapter 6

5

While Statement SYNTAX

while ( x-- > 0 ) { .

. // loop body }

Loop body can be a single statement, a null

statement, or a block

Page 6: Programming in C++ Chapter 6

6

When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body

WHILE LOOP

FALSE

TRUE

body statements

Expression

Page 7: Programming in C++ Chapter 6

7

Count-controlled loops contain ■ An initialization of the loop control

variable ■ An expression to test if the proper

number of repetitions has been completed

■ An update of the loop control variable to be executed with each iteration of the body

Count-Controlled Loops

Page 8: Programming in C++ Chapter 6

8

int count; // Loop-control variable!count = 4; // Initialize loop variable!!while( count > 0 ) ! // Test expression!{! cout << count << endl; // Repeated action!! count--; ! // Update loop variable!}!cout << “Done” << endl;!

Count-Controlled Loop Example

Page 9: Programming in C++ Chapter 6

9

Count-controlled Loop

int count;!

count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl;!!!

count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT

count

Page 10: Programming in C++ Chapter 6

10

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!

cout << “Done” << endl;

OUTPUT

count

4

Page 11: Programming in C++ Chapter 6

11

Count-controlled Loop int count;!!

count = 4; ! ! !

!!while(count > 0) !TRUE !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT

count

4

Page 12: Programming in C++ Chapter 6

12

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

count

4

Page 13: Programming in C++ Chapter 6

13

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!

}!cout << “Done” << endl;!

OUTPUT 4

count

3

Page 14: Programming in C++ Chapter 6

14

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) !TRUE ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

count

3

Page 15: Programming in C++ Chapter 6

15

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

3

count

3

Page 16: Programming in C++ Chapter 6

16

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

3

count

2

Page 17: Programming in C++ Chapter 6

17

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) !TRUE !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

3

count

2

Page 18: Programming in C++ Chapter 6

18

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;

OUTPUT 4

3 2

count

2

Page 19: Programming in C++ Chapter 6

19

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

3 2

count

1

Page 20: Programming in C++ Chapter 6

20

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) !TRUE !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;

OUTPUT 4

3 2

count

1

Page 21: Programming in C++ Chapter 6

21

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

3 2 1

count

1

Page 22: Programming in C++ Chapter 6

22

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) ! ! !!{! cout << count << endl; !!! count --; ! ! !!

}!cout << “Done” << endl;!

OUTPUT 4

3 2 1

count

0

Page 23: Programming in C++ Chapter 6

23

Count-controlled Loop int count;!!count = 4; ! ! !

!!!while(count > 0) !FALSE!{! cout << count << endl; !!! count --; ! ! !!}!cout << “Done” << endl;!

OUTPUT 4

3 2 1

count

0

Page 24: Programming in C++ Chapter 6

24

Count-controlled Loop int count;!!count = 10; ! ! !

!!!while(count >= 0)! ! !!{! cout << count << endl; !!! count --; ! ! !!}!

cout << “Done” << endl;!

OUTPUT 4

3 2 1 Done

count

0

Page 25: Programming in C++ Chapter 6

25

Types of Event-Controlled Loops

●  Sentinel controlled Keep processing data until a special value that

is not a possible data value is entered to indicate that processing should stop

●  End-of-file controlled Keep processing data as long as there is more

data in the file

●  Flag controlled Keep processing data until the value of a flag

changes in the loop body 25

Page 26: Programming in C++ Chapter 6

26

Examples of Kinds of Loops

Count controlled loop Read exactly 100 blood pressures from a file

End-of-file controlled loop

Read all the blood pressures from a file no matter how many are there

26

Page 27: Programming in C++ Chapter 6

27

Examples of Kinds of Loops

Sentinel controlled loop

Read blood pressures until a special value selected by you (like -1) is read

Flag controlled loop

Read blood pressures until a dangerously high BP(200 or more) is read

27

Page 28: Programming in C++ Chapter 6

28

A Sentinel-controlled Loop

● Requires a “priming read” – reading until EndOfFile requires priming

read too!

● A priming read is the reading of

one set of data before the loop to initialize the variables in the expression

Page 29: Programming in C++ Chapter 6

29

// Sentinel controlled loop!const int END_VALUE = -1;!total = 0;!!cout << “Enter a blood pressure(-1 to stop) ”;!cin >> thisBP;!!while ( thisBP != END_VALUE) !// While not sentinel!{! total = total + thisBP;! cout << “Enter a blood pressure(-1 to stop)”;! cin >> thisBP;!}!cout << total;!

Page 30: Programming in C++ Chapter 6

30

End-of-File Controlled Loop

● Uses the fact that a file goes into the fail state when you try to read a data value beyond the end of the file to control the loop

Page 31: Programming in C++ Chapter 6

31

ifstream bpFile("infile.txt");!double thisBP;!int total = 0;!!bpFile >> thisBP; // Priming read!!!while(bpFile) ! // While last read successful!{! total += thisBP;! bpFile >> thisBP; ! // Read another!}!!cout << total;!!

Page 32: Programming in C++ Chapter 6

32

total = 0;!!cout << “Enter blood pressure “! << “(Ctrl-Z to stop)”;!cin >> thisBP; // Priming read!!while(cin) ! // While last read successful!{! total = total + thisBP;! cout << “Enter blood pressure”;! cin >> thisBP; // Read another!}!cout << total;!

Page 33: Programming in C++ Chapter 6

33

Flag-controlled Loops

●  Initialize a flag(to true or false) ● Use meaningful name for the flag ● A condition in the loop body

changes the value of the flag ● Test for the flag in the loop test

expression

Page 34: Programming in C++ Chapter 6

34

!countGoodReadings = 0;!isSafe = true; ! // Initialize Boolean flag!!while(isSafe)!{!! cin >> thisBP;! if (thisBP >= 200)! isSafe = false; // Change flag value!! else! countGoodReadings++;!}!!cout << countGoodReadings << endl;

Page 35: Programming in C++ Chapter 6

35

Common Loop Uses

●  Count all data values

●  Count special data values

●  Sum data values

●  Keep track of current and previous values

Page 36: Programming in C++ Chapter 6

36

Loop Design Considerations - What is the condition that terminates the loop? - How should the condition be initialized? - How should the condition be updated? - What guarantees this condition will eventually occur? - What is the process to be repeated? - How should the process be initialized? - How should the process be updated? - What is the state of the program on exiting the loop? - Are "boundary conditions" handled correctly?

Page 37: Programming in C++ Chapter 6

37

initialize outer loop while (outer loop condition) { . . .

initialize inner loop

while(inner loop condition) { inner loop processing and update }

. . . }

Nested Loops

37

Page 38: Programming in C++ Chapter 6

38

Designing Nested Loops

●  Begin with outer loop ●  When you get to where the inner loop

appears, make it a separate module and come back to its design later

Page 39: Programming in C++ Chapter 6

39

Loop Testing and Debugging ●  Test data should test all sections of program!

●  Beware of infinite loops -- program doesn’t stop

●  Check loop termination condition, and watch for “off-by-1” bugs(OBOBs)

●  Use get function for loops controlled by detection of ‘\n’ character

●  Use algorithm walk-through to verify pre- and postconditions

●  Trace execution of loop by hand with code walk-through

●  Use a debugger to run program in “slow motion” or use debug output statements