Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

29
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

description

Clearly Visual Basic: Programming with Visual Basic 2008 Over and Over Again Repetition structure (loop) –Used to process one or more program instructions repeatedly, until some condition is met –Can be either a pretest loop or a posttest loop Pretest loop –Loop condition is evaluated before the instructions within the loop are processed Posttest loop –Evaluation occurs after the instructions within the loop are processed 3

Transcript of Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Page 1: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Clearly Visual Basic: Programming with Visual Basic 2008

Chapter 13 How Long Can This Go On?

Page 2: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Clearly Visual Basic: Programming with Visual Basic 2008

Objectives

• Write a pretest loop using the Do…Loop statement• Utilize counter and accumulator variables• Refresh the screen• Delay program execution• Show a pretest loop in both pseudocode and a

flowchart• Display a dialog box using the InputBox function

2

Page 3: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Clearly Visual Basic: Programming with Visual Basic 2008

Over and Over Again

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

repeatedly, until some condition is met – Can be either a pretest loop or a posttest loop

• Pretest loop– Loop condition is evaluated before the instructions

within the loop are processed• Posttest loop

– Evaluation occurs after the instructions within the loop are processed

3

Page 4: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

4Clearly Visual Basic: Programming with Visual Basic 2008

Page 5: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Over and Over Again (continued)

• Figure 13-2 – Shows the modified problem specification along with

the modified algorithm• Figure 13-3

– Shows another problem specification and algorithm involving Rob

• Figures 13-4 and 13-5– Shows a modified version of the previous problem

specification

5Clearly Visual Basic: Programming with Visual Basic 2008

Page 6: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

6Clearly Visual Basic: Programming with Visual Basic 2008

Page 7: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

7Clearly Visual Basic: Programming with Visual Basic 2008

Page 8: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

8Clearly Visual Basic: Programming with Visual Basic 2008

Page 9: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

9Clearly Visual Basic: Programming with Visual Basic 2008

Page 10: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Clearly Visual Basic: Programming with Visual Basic 2008 10

The Do … Loop Statement

• Do…Loop statement – Used to code both a pretest loop and a posttest loop

• Figure 13-6 – Shows syntax of the Do…Loop statement

• Keyword While – Indicates that the loop instructions should be

processed while the condition is true• Keyword Until

– Indicates that the loop instructions should be processed until the condition becomes true

Page 11: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

11Clearly Visual Basic: Programming with Visual Basic 2008

Page 12: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

12Clearly Visual Basic: Programming with Visual Basic 2008

Page 13: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

The Do … Loop Statement (continued)

• intNumber variable – Called a counter variable

• Counter variables – Always numeric variables – Typically assigned a beginning value of either 0 or 1,

depending on the value required by the application• Updating (incrementing)

– Adding a number to the value stored in the counter variable

13Clearly Visual Basic: Programming with Visual Basic 2008

Page 14: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Clearly Visual Basic: Programming with Visual Basic 2008

14

Page 15: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

My Dream Car-Version 1 Application

• Figure 13-8 – Shows the interface for the My Dream Car-Version 1

application• Refresh method

– Will ensure that any code appearing before it that affects the interface’s appearance is processed

– Syntax: Me.Refresh()• Sleep method

– Used to delay program execution– Syntax

• System.Threading.Thread.Sleep(milliseconds)

15Clearly Visual Basic: Programming with Visual Basic 2008

Page 16: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

16Clearly Visual Basic: Programming with Visual Basic 2008

Page 17: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Flowcharting the Click Me Button’s Event Procedure

• Figure 13-11 – Shows flowchart for the Click Me button’s Click event

procedure• You mark the flowline leading to the true path with a

“T” (for True)• You mark the flowline leading to the false path with an

“F” (for False)

17Clearly Visual Basic: Programming with Visual Basic 2008

Page 18: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

18Clearly Visual Basic: Programming with Visual Basic 2008

Page 19: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

My Dream Car-Version 2 Application

• The lblMessage control’s Visible property – Set to True in the Properties window– The picCar control’s Visible property is set to False

• Figure 13-12 – Shows the algorithm for the Click Me button’s Click

event procedure

19Clearly Visual Basic: Programming with Visual Basic 2008

Page 20: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

20Clearly Visual Basic: Programming with Visual Basic 2008

Page 21: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

The Sales Express Application

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

21Clearly Visual Basic: Programming with Visual Basic 2008

Page 22: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

22Clearly Visual Basic: Programming with Visual Basic 2008

Page 23: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

23Clearly Visual Basic: Programming with Visual Basic 2008

Page 24: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

The InputBox Function

• Displays one of the standard dialog boxes available in Visual Basic

• Empty string – Represented by the String.Empty constant

• Figure 13-16 – Shows basic syntax of the InputBox function

24Clearly Visual Basic: Programming with Visual Basic 2008

Page 25: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

25Clearly Visual Basic: Programming with Visual Basic 2008

Page 26: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

The InputBox Function (continued)

• Figure 13-18– Shows a sample testing chart for the Sales Express

application• Runtime error

– Occurs while an application is running• Before using a variable as the divisor in an

expression:– Verify that the variable does not contain the number

0

26Clearly Visual Basic: Programming with Visual Basic 2008

Page 27: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

27Clearly Visual Basic: Programming with Visual Basic 2008

Page 28: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Summary

• Use a repetition structure, or loop – To repeatedly process one or more program

instructions• Loop

– Can be either a pretest loop or a posttest loop• Do…Loop statement

– Can be used to code a pretest loop• Counter and accumulator variables

– Should be assigned a value outside the loop, but updated within the loop

28Clearly Visual Basic: Programming with Visual Basic 2008

Page 29: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?

Summary (continued)

• Loop condition – Represented by a diamond in a flowchart

• Priming read– Gets only the first input item from the user

• InputBox function– Can be used to display a dialog box that contains a

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

• If a variable is used as the divisor in an expression: – Always verify that the variable does not contain the

number 0

29Clearly Visual Basic: Programming with Visual Basic 2008