tools for structured and object oriented design - dowhile structures

22
DOWHILE Control Structure Tools for Structured and Object-Oriented Design

Upload: andrew-carts

Post on 02-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Tools for Structured and Object Oriented Design - DOWHILE Structures

TRANSCRIPT

Page 1: Tools for Structured and Object Oriented Design - DOWHILE Structures

DOWHILE Control Structure

Tools for Structured and Object-Oriented Design

Page 2: Tools for Structured and Object Oriented Design - DOWHILE Structures

Objectives

• Indentify, and use in program design, counters, accumulators, and program loops

• Design, and identify the characteristics of, a simple counter-controlled program loop.

• Identify, and use in program design, the DOWHILE control structure.

• Identify, and use in program design, the preparation program flowcharting symbol

Page 3: Tools for Structured and Object Oriented Design - DOWHILE Structures

Objectives (continued)

• Design programs using counter-controlled loops• Design programs using header record logic• Define the term proper program

Page 4: Tools for Structured and Object Oriented Design - DOWHILE Structures

Understanding the Advantages of Looping

• When you use a loop, the structure that repeats actions while some condition continues, within a computer program, – you can write one set of instructions that operates on

multiple, separate sets of data• For example, the advantage of having a computer perform

payroll calculations is that all of the deduction instructions need to be written only once and can be repeated over and over again for each paycheck

Page 5: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a While Loop with a Loop Control Variable

• You learned that almost every program has a main loop, or a basic set of instructions that is repeated for every record

• The main loop is a typical loop—within it, you write one set of instructions that executes repeatedly while records continue to be read from an input file

Page 6: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a While Loop with a Loop Control Variable (continued)

• In addition to this main loop, loops also appear within subroutines

– Used any time you need to perform a task several times and don’t want to write identical or similar instructions over and over

Page 7: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a While Loop with a Loop Control Variable (continued)

Page 8: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a While Loop with a Loop Control Variable (continued)

• Three steps that must occur in every loop are as follows:

1. You must initialize a variable that will control the loop. The variable in figure 6-3 is named rep.

2. You must compare the variable to some value that controls whether the loop continues or stops. In this case, you compare rep to the value 5.

3. Within the loop, you must alter the variable that controls the loop. Thus, you alter rep by adding 1 to it.

Page 9: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a While Loop with a Loop Control Variable (continued)

• On each pass through the loop, the value in the rep variable determines whether the loop will continue

• Therefore, variables like rep are known as loop control variables

• To stop a loop, you compare the loop control value to a sentinel value

• The statements that execute within a loop are known as the loop body

Page 10: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a Counter to Control Looping

Page 11: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a Counter to Control Looping (continued)

• A counter is any numeric variable you use to count the number of times an event has occurred; in figure 6-5, you need a counter to keep track of how many labels have been printed at any point

• Each time you read an employee record, the counter variable is set to 0

• Then every time a label is printed, you add 1 to the counter• Adding 1 to a variable is called incrementing the variable

Page 12: Tools for Structured and Object Oriented Design - DOWHILE Structures

Using a Counter to Control Looping (continued)

Page 13: Tools for Structured and Object Oriented Design - DOWHILE Structures

Looping with a Variable Sentinel Value

• Sometimes you don’t want to be forced to repeat every pass through a loop the same number of times

• For example, instead of printing 100 labels for each employee, you might want to vary the number of labels based on how many items a worker actually produces

• To write a program that produces an appropriate number of labels for each employee, you can make some minor modifications to the original label-making program

Page 14: Tools for Structured and Object Oriented Design - DOWHILE Structures

Looping with a Variable Sentinel Value (continued)

• For example, the input file variables have changed; you must declare a variable for an inLastProduction field

• Additionally, you might want to create a numeric field named labelsToPrint that can hold a value equal to 110% of a worker’s inLastProduction

• The major modification to the original label-making program is in the question that controls the label-producing loop

Page 15: Tools for Structured and Object Oriented Design - DOWHILE Structures

Looping with a Variable Sentinel Value (continued)

• Instead of asking if labelCounter < 100, you now can ask if labelCounter < labelsToPrint

• The sentinel or limit value can be a variable like labelsToPrint just as easily as it can be a constant like 100

Page 16: Tools for Structured and Object Oriented Design - DOWHILE Structures

Flowchart and Pseudocode for Label-Making mainLoop()

Page 17: Tools for Structured and Object Oriented Design - DOWHILE Structures

Looping by Decrementing

• Rather than incrementing a loop control variable until it passes some sentinel value, sometimes it is more convenient to reduce a loop control variable on every cycle through a loop

• Decreasing a variable by one is called decrementing the variable

Page 18: Tools for Structured and Object Oriented Design - DOWHILE Structures

DOWHILE Example

Page 19: Tools for Structured and Object Oriented Design - DOWHILE Structures

Do While Example

Page 20: Tools for Structured and Object Oriented Design - DOWHILE Structures

Do While Example

Page 21: Tools for Structured and Object Oriented Design - DOWHILE Structures

DO WHILE Pseudocode

Page 22: Tools for Structured and Object Oriented Design - DOWHILE Structures

DO WHILE Example