Clearly Visual Basic: Programming with Visual Basic 2010 2nd Edition Chapter 14 Let Me Count the Ways (Counter-Controlled Loops)
Objectives After studying Chapter 14, you should be able to: Code a counter-controlled pretest loop using the For…Next statement Play an audio file while an application is running Calculate a periodic payment using the Financial.Pmt method Concatenate strings Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
When Will It Stop? Counter-controlled loop Loop whose instructions you want processed a precise number of times Can be either a pretest loop or a posttest loop Coding a posttest counter-controlled loop Use the Do...Loop statement Coding a Pretest counter-controlled loop Use either the Do…Loop statement or the For…Next statement Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
When Will It Stop? (cont’d.) For…Next syntax Shown in Figure 14-1 on next slide CounterVariableName Name of numeric variable Can be declared in a Dim statement To have procedure scope Variable declared in the For clause: Has block scope and can be used only within the loop Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
When Will It Stop? (cont’d.) startValue, endValue, stepValue Numeric items in the For clause Control the number of times loop is processed Can be positive or negative, integer, or non-integer Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 14-1 Syntax, example, and processing tasks for the For…Next statement (continues) Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 14-1 Syntax, example, and processing tasks for the For…Next statement (cont’d.) Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 14-2 Processing steps for the example in Figure 14-1 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Spaceship-Version 1 Application Figure 14-3 Shows the interface for the Spaceship-Version 1 application Before coding the Spaceship-Version 1 application: View the Ch14-Spaceship video Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Spaceship-Version 1 Application (cont’d.) Figure 14-3 Interface for the Spaceship-Version 1 application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Spaceship-Version 2 Application Display the numbers 3, 2, and 1 in the lblCountToBlastOff control Figure 14-5 For…Next statement entered in the Go button’s Click event procedure in the Spaceship-Version 2 application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Hey, Turn That Noise Down! To have an application play an audio file while it is running: Use the syntax My.Computer.Audio.Play(fileName) Visual Basic’s My feature Exposes a set of commonly used objects to the programmer Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
The Monthly Payment Calculator Application Figure 14-6 Shows the interface for the Monthly Payment Calculator application Figure 14-7 Shows the application’s output, processing, and input items, along with its algorithm Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
The Monthly Payment Calculator Application (cont’d.) Figure 14-6 Interface for the Monthly Payment Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 14-7 Output, processing, input, and algorithm for the Monthly Payment Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
The Financial.Pmt Method Calculates the periodic payment on a loan Figure 14-8 Shows the method’s basic syntax Lists the meaning of each of the three arguments included in the syntax Gives examples Can also be used to calculate periodic payment on an investment Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 14-8 Basic syntax and examples of the Financial.Pmt method Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
But They Said There Were No Strings Attached Concatenation operator Represented by ampersand (&) Used to concatenate (connect or link) strings Must use a space before and after the ampersand Figure 14-9 Shows syntax and examples of using the concatenation operator Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 14-9 Syntax and examples of concatenating strings Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Summary The For…Next statement Variable declared in a For clause Provides a convenient way to code a counter-controlled loop Variable declared in a For clause Has block scope Can be used only by the For…Next loop The For…Next statement’s counter variable must be numeric Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Summary (cont’d.) The syntax My.Computer.Audio.Play(fileName) Can be used to play an audio file while an application is running Financial.Pmt method Used to calculate a periodic payment on a loan or investment Concatenation operator (&) Used to connect strings Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition