Download - Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Transcript
Page 1: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Clearly Visual Basic:Programming with Microsoft

Visual Basic 2012

Chapter 12: How Long Can This Go On?(Pretest Loops)

Page 2: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Chapter Objectives

After studying Chapter 12, you should be able to:• Write a looping condition and its opposing loop exit

condition• Show a pretest loop in both pseudocode and a flowchart• Write a pretest loop using the Do…Loop statement• Utilize counter and accumulator variables• Refresh the screen• Delay program execution• Display a dialog box using the InputBox function• Abbreviate assignment statements using the arithmetic

assignment operators

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 2

Page 3: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 3

• Repetition structure (loop)– Used to repeatedly process one or more program

instructions– The loop’s condition determines if and for how long the

instructions are repeated• The requirement for repeating the instructions is referred

to as the looping condition because it indicates when the computer should continue “looping” through the instructions

• The requirement for not repeating the instructions is referred to as the loop exit condition because it tells the computer when to exit (or stop) the loop

Page 4: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 4

A repetition structure can be either a pretest loop or a posttest loop

Page 5: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 5

Figure 12-1 Examples of looping and loop exit conditions

Pretest loopLoop condition is evaluated before the instructions within the loop are processedPosttest loopEvaluation occurs after the instructions within the loop are processed

Page 6: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 6

Depending on the result of the evaluation, the instructions in a pretest loop may never be processedThe instructions in a posttest loop, however, will always be processed at least onceOf the two types of loops the pretest loop is the most commonly usedYou will learn about pretest loops in this chapter and in chapter 14. Postttest loops are covered in chapter 13

Page 7: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 7

Figure 12-2 A problem that requires the sequence and selection structures

Figure 12-3 A problem that requires the sequence and repetition structures

• Loop Body is the statements between the repeat and end-repeat clauses

Page 8: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 8

Figure 12-4 Another problem that requires the sequence and selection structures

Figure 12-5 A problem that requires the sequence, selection, and repetition structures

Page 9: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Over and Over Again (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 9

Figure 12-6 Two versions of the modified algorithm from Figure 12-5

Page 10: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 10

• Use the Do…Loop statement to code both pretest and posttest loops

• Statements begin with the Do clause and end with the Loop clause• Between both clauses, instructions the computer is

to repeat are entered• Referred to as the loop body

Page 11: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 11

Figure 12-7 Syntax and examples of the Do…Loop statement for a pretest loop

Page 12: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

• Counter• Used to count something• Always numeric variables• Initializing counter variables

• Typically assigned a beginning value of either 0 or 1, depending on the value required by the application

• Updating • Adding a number to the value stored in the counter

variable is called incrementing• Subtracting a number from the value stored in the

counter variable is called decrementing

Counter Variables

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 12

Page 13: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Counter Variables (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 13

Figure 12-8 Code and processing steps for Example 1 in Figure 12-7

Page 14: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Counter Variables (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 14

Repetition structures use counters to count such things as the number of employees paid in a week or the number of positive numbers entered by the user, for the purpose of counting we use integer variables to countIn page 268 the integer variable intNum is referred to as a counter(work the next exercise)

Page 15: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 15

Exercise1Write a statement or a set of statements to accomplish each of the followinga) Sum the odd integers between 1 and 15 using a Do While…Loop

repetition statement. Use integer variables sum and countb) Sum the squares of the even integers between 1 and 15 using a

Do While…Loop repetition statement. Use integer variables sum and count and initialize them to 0 and 2, respectively

c) Display the numbers from 5 to 1 in txtResult using a Do Until and Integer counter variable counterIndex. Initialize counterIndex to 5

d) Repeat exercise in part c using a Do While…Loop

Page 16: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 16

Exercise2Write a Visual Basic statement to accomplish each of the following tasksa) Declare variables sum and number to be of type integerb) Assign 1 to variable numberc) Assign 0 to the variable sumd) Total variables number and sum and assign the result to variable

sume) Display “The sum is: “ followed by the value of the variable

sum in the lblResult

Page 17: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 17

Exercise 3Combine the statements that you wrote in the previous exercise into an application that calculates and displays the sum o f integers from 1 to 10. Use a Do While… loop to loop through the calculations and increment statements. The loop should terminate when the value of control variable number becomes 11.

Page 18: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 18

Exercise 4Identify and correct the error(s) in each of the following (you may need to add code):a) The following loop should total the values from 1 to 50. Assume

that value is 50Do While value >= 0

Sum += value Loop

b) The following code should display the squares of 1 to 10 in listBoxResultDim number As Integer = 1

Do While number < 10listBoxResult.Items.Add(number ^ 2)End While

Page 19: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Do…Loop Statement

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 19

c) This segment should display the integers from 888 to 1000 in ListBoxResult. Initialize variable value to 888

Dim value As Integer = 888Do While value < = 1000

Value -= 1loop

Page 20: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Counter Variables (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 20

Figure 12-9 Example of a partial game program that uses a counter

Page 21: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Counter Variables (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 21

Figure 12-10 Interface for the Cheerleader application

• The interface for the Cheerleader application uses a repetition structure to make the “Go Team!” message blink several times when the user clicks the Click Here button

Page 22: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Counter Variables (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 22

Figure 12-11 Algorithm (pseudocode and flowchart) for the Click Here button (Continues)

• Refresh method• Ensures computer processes previous lines of code that

affect the form’s appearance• Syntax: Me.Refresh()

• Sleep method• Used to delay program execution• Syntax: System.Threading.Thread.Sleep(milliseconds)

Page 23: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Counter Variables (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 23

Figure 12-11 Algorithm (pseudocode and flowchart) for the Click Here button

Figure 12-12 btnClickHere_Click procedure in the Cheerleader application

Page 24: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

My Dream Car Application

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 24

• The interface for the My Dream Car application uses a repetition structure to drag the car image from the left edge of the form to the center of the form

• The car image is contained in the picCar control, whose Visible property is set to False in the Properties window

Figure 12-13 Interface for the My Dream Car application

Page 25: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

My Dream Car Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 25

Figure 12-15 btnShow_Click procedure in the My Dream Car application

Figure 12-14 Algorithm for the Show the Car button’s Click event procedure

Page 26: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 26

• Accumulator variable • Numeric variable used for accumulating something• Assigned a value outside the loop• Updated within the loop• Incremented by an amount that varies

Page 27: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 27

• Priming read• Used to prepare (prime) the loop• Initializes the loop condition by providing its first

value• Update read

• Allows user to update the value of the input item• Infinite (endless) loop

• Loop that has no way to end• To force an infinite loop to end

• Click Debug on the menu bar, then Stop Debugging

Page 28: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 28

Figure 12-17 Interface for the Sales Express application

Figure 12-16 Problem specification and algorithm for the Sales Express application

Page 29: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 29

• Rather than using a text box, the application will use an input dialog box• An InputBox function returns a value depending on

the button the user chooses• If the user clicks the OK button, the function returns

the value contained in the input area of the dialog box; the return value is always treated as a string

• If the user clicks either the Cancel button in the dialog box or the Close button on the dialog box’s title bar, the function returns an empty string

• The empty string is represented by the String.Empty constant

Page 30: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 30

Figure 12-19 Syntax and examples of the InputBox function

Figure 12-18 Example of an input dialog box created by the InputBox function

Page 31: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 31

Figure 12-20 btnCalc_Click procedure in the Sales Express application

Page 32: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

The Sales Express Application (Cont.)

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 32

Figure 12-22 Sample run of the Sales Express application

Figure 12-21 Test data chart for the Sales Express application

Page 33: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Can I Abbreviate That Assignment Statement?

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 33

Figure 12-23 Syntax and examples of the arithmetic assignment operators

• Use the arithmetic assignment operators to abbreviate an assignment statement that contains an arithmetic operator

Page 34: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Summary

• Use a repetition structure (loop) to repeatedly process one or more program instructions

• Repetition structures can be either a pretest loop or a posttest loop - Pretest loops are more commonly used

• Use the Do…Loop statement to code a pretest loop – The While keyword in the statement’s condition indicates

that the loop instructions should be processed while (as long as) the condition is true

– The Until keyword in the statement’s condition indicates that the loop instructions should be processed until the condition becomes true

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 34

Page 35: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Summary (Cont.)

• Counters and accumulators must be initialized and updated– The initialization is done outside of the loop that uses

the counter or accumulator, and the updating is done within the loop

– Counters are updated by a constant amount, whereas accumulators are usually updated by an amount that varies

• In a flowchart, the loop condition is represented by the decision symbol, which is a diamond

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 35

Page 36: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Summary (Cont.)

• Use the Refresh method to refresh (redraw) the form– The method’s syntax is Me.Refresh()

• Use the Sleep method to delay program execution– The method’s syntax is

System.Threading.Thread.Sleep(milliseconds)• The priming read appears above the loop that it

controls• The InputBox function displays an input dialog box that

contains a message, an OK button, a Cancel button, and an input area

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 36

Page 37: Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

Summary (Cont.)

• A run time error occurs, you can stop the application by clicking DEBUG on the menu bar and then clicking Stop Debugging

• Before using a variable as the divisor in an expression, verify that the variable does not contain the number 0– Dividing by 0 is mathematically impossible and will cause

a run time error to occur• Use an arithmetic assignment operator to abbreviate an

assignment statement that has the following format, in which variableName is the name of the same variable:– variableName = variableName arithmeticOperator value

Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 37