Clearly Visual Basic Programming with Visual Basic 2010
Clearly Visual Basic: Programming with Visual Basic 2010 2 nd 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 2 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 3 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
When Will It Stop? (cont’d. ) For…Next syntax Shown in Figure 14 -1 on next slide Counter. Variable. 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 4 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
When Will It Stop? (cont’d. ) start. Value, end. Value, step. Value Numeric items in the For clause Control the number of times loop is processed Can be positive or negative, integer, or non-integer 5 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 14 -1 Syntax, example, and processing tasks for the For…Next statement (continues) 6 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 14 -1 Syntax, example, and processing tasks for the For…Next statement (cont’d. ) 7 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 14 -2 Processing steps for the example in Figure 14 -1 8 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 Ch 14 -Spaceship video 9 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Spaceship-Version 1 Application (cont’d. ) Figure 14 -3 Interface for the Spaceship-Version 1 application 10 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Spaceship-Version 2 Application Display the numbers 3, 2, and 1 in the lbl. Count. To. Blast. Off control Figure 14 -5 For…Next statement entered in the Go button’s Click event procedure in the Spaceship-Version 2 application 11 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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(file. Name) Visual Basic’s My feature Exposes a set of commonly used objects to the programmer 12 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 13 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
The Monthly Payment Calculator Application (cont’d. ) Figure 14 -6 Interface for the Monthly Payment Calculator application 14 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 14 -7 Output, processing, input, and algorithm for the Monthly Payment Calculator application 15 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 16 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 14 -8 Basic syntax and examples of the Financial. Pmt method 17 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 18 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 14 -9 Syntax and examples of concatenating strings 19 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Summary The For…Next statement 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 20 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Summary (cont’d. ) The syntax My. Computer. Audio. Play(file. Name) 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 21 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
- Slides: 21