Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 7: Repetition
Last change: February 13 2016 18:47:47.
Next>>

Chapter 7
Repetition

One of the most powerful features of the computer is its capability to execute a group of instructions repetitively while the data handled by these instructions change. With this capability, many tasks that are repetitive in nature can be handled with much shorter code and greater flexibility. Imagine a payroll routine that computes for each employee the gross pay, various deductions, and the net pay. The steps to handle each employee’s pay are basically the same. Only the data—the employee and the pay—in question change. Without the capability to execute the group of instructions repetitively, the same code will have to be repeated for each employee. The length of code will be sizable, to say the least. Worse yet, the number of repetitions in code, which vary with the number of employees to be paid, will have to be known beforehand. Each time the number of employees changes, the program will have to be modified—an extremely tedious task.
Many of the problems (much more than can be imagined) handled by the computer are repetitive in nature. To list a few, populating a list box or combo box, reversing a string, computing the square root of a number (searching for the root of any function in general), and listing the content of a file  all require repeating a selected group of instructions.
In VB, there are several structures by which you can construct code for repetition. In each of these structures, the repetition starts with a keyword, such as Do or For, in an opening statement and ends with another keyword, such as Loop or Next, in the closing statement. Statements enclosed physically inside the two statements are repeated until a certain condition is satisfied. Because the execution of the group of statements starts from the opening statement top down, ends at the closing statement, and starts over again from the opening statement, the repetition looks like a loop and is commonly so referenced. This chapter provides detailed discussions of these structures.
After completing this chapter, you should be able to:

Last change: February 13 2016 18:47:49.
Next>>