290 likes | 316 Views
Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 13 How Long Can This Go On?. Objectives. Write a pretest loop using the Do…Loop statement Utilize counter and accumulator variables Refresh the screen Delay program execution
E N D
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
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 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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 todelay program execution • Syntax • System.Threading.Thread.Sleep(milliseconds) Clearly Visual Basic: Programming with Visual Basic 2008
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) Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008
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 Clearly Visual Basic: Programming with Visual Basic 2008