Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.

Similar presentations


Presentation on theme: "Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops."— Presentation transcript:

1 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 1 Chapter 5: The Repetition Process in Visual Basic

2 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 2 The Repetition Process The capability to repeat one or more statements as many times as necessary is what really sets a computer apart from other devices All loops have two parts: –the body of the loop (the statements being repeated) –a termination condition that stops the loop Failure to have a valid termination condition can lead to an endless loop

3 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 3 Types of Loops There are three types of loops –event-driven –determinate –indeterminate Event-driven loops are repeated by the user causing an event to occur Determinate loops repeat a known number of times Indeterminate loops repeat an unknown number of times Variables should be initialized before being used in a loop

4 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 4 Event-Driven Loops Event-driven loops are repeated by user causing an event to occur Variable scope is important for loops; Variables in event procedures are local and are reset to zero when procedure terminates Variables defined at form level are known to all procedures and retain their value between events Form-level variables are declared in the Declarations procedure of the General object Static variables retain their value between events but are local to event procedure Declared with Static keyword

5 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 5 Form-level Variables

6 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 6 Use of AddItem and Clear Methods The AddItem method is used to add items to a list box at run time Form of AddItem method list1.Additem string Always add a string to list box since it contains text The Clear method clears a list box list1.Clear

7 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 Code to Sum a Series of Numbers Private Sub cmdCalc_Click() Dim intTheValue As Integer intTheValue = CInt(txtTheValue.Text) intSum = intSum + intTheValue ‘intSum is form level intNumValues = intNumValues + 1 ‘form level txtSum.Text = Str(intSum) txtNumValues.Text = Str(intNumValues) lstEntries.AddItem str(intTheValue) txtTheValue.Text = "” txtTheValue.SetFocus End Sub

8 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 8 Determinate Loops Using For- Next Loop Best way to create a determinate loop is to use a For- Next Loop Form of For-Next Loop: For variable = start value to end value Step change value statements that compose body of loop Next variable where variable = the counter variable in the loop start value = the beginning value of the counter variable end value = the ending value of the counter variable change value = the amount the counter variable changes each time through the loop Next variable = the end of the For loop

9 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 9 Example of For-Next Loop Counter Variable Beginning Value Ending Change Value intSum = intSum + intCounter Body of Loop ForintCounter = 1 to 10 Step 1 ForStatement Next intCounter NextStatement

10 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 Code To Sum using For-Next Loop Private Sub cmdCalc_Click() Dim intTheValue as Integer, intSum as Integer Dim intNumValues as Integer, intCounter as Integer intSum = 0 ‘Initialize Sum to Zero If txtNumValues.Text = "" Then ’Number not entered Msgbox "Please enter number of values to be summed“ Exit Sub ’Do not go into loop End if intNumValues = CInt(txtNumValues.Text) For intCounter = 1 to intNumValues int TheValue = CInt(InputBox("Enter next value")) intSum = intSum + intTheValue lstEntries.AddItem Str(intTheValue) ‘Add to list Next txtSum.Text = Str(intSum) lstEntries.AddItem "Sum is " & Str(intSum) End Sub

11 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 Printing a List To print the contents of listbox, use the Listcount and List() properties of the list box Listcount is the number of items in list box but List() property runs from 0 to Listcount -1 List(intCounter) is equal to the contents of the corresponding list box Code to print contents of list box to Immediate Window: For intCounter = 0 to lstEntries.ListCount - 1 Debug.Print lstEntries.List(intCounter) Next

12 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 12 Indeterminate Loops Indeterminate loops run for an unknown number of repetitions until a condition is true or while a condition is true Four types of indeterminate loops –Until loop with termination condition before body of loop –While loop with termination condition before body of loop –Until loop with termination condition after body of loop –While loop with termination condition after body of loop Pre-Test loops have termination condition before loop body Post-test loops have termination condition after loop body

13 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 13 Form of Pre- and Post-Test Loops The form of the pre-test loops is: Do Until (or While) condition body of loop Loop The form of the post-test loops is: Do body of loop Loop Until (or While) condition

14 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 Pre and Post-Test Loops

15 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 Processing an Unknown Number of Values from a File A data file is a collection of data stored on magnetic or optical secondary storage in the form of records. A record is a collection of one or more data items that are treated as a unit. Files are identified by the computer’s operating system with file names assigned by the user. Files are important to processing data into information because they provide a permanent method of storing large amounts of data that can be input whenever needed. Three types of files: sequential access, database, and direct access files We will use sequential access files as input.

16 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 16 Using Sequential Access Files Sequential access files must be read in same order as they are created so they replicate the action of entering data from a keyboard. The number of records on a sequential access file is often unknown, but there is an invisible binary marker at the end of the file called the EOF (end of file) marker. Use a Do While loop or a Do Until loop to input data from sequential access file. Loops can input data until the EOF marker is encountered (or while it has not been encountered). Create sequential access files by using a text editor such as Notepad.

17 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 17 Opening and Inputting Data from a File Files must be opened with the Open statement: Open “filename" for Input as #n To input data from a file, use the Input #n, statement Input #n, list of variables Files must be closed at end of procedure Close #n Using an Until loop to input data from file Do Until EOF(n) Input #n, list of variables Loop

18 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 18 Code to Input and Sum values from File Private Sub cmdCalc_Click() Dim intTheValue As Integer, intSum As Integer Dim intNumValues As Integer Open "a:\SumData.txt" For Input As #10 intSum = 0 intNumValues = 0 Do Until EOF(10) ’Input to end of file Input #10, intTheValue intSum = intSum + intTheValue intNumValues = intNumValues + 1 lstEntries.AddItem Str(intTheValue) Loop txtNumValues.Text = Str(intNumValues) txtSum.Text = Str(intSum) lstEntries.AddItem "Sum is " & str(intSum) Close #10 End Sub

19 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 19 The Combo Box A combo box is a combination of a text box and a list box. It has a text box portion that is displayed at all times and a drop-down list of items that can be displayed by clicking on the down arrow. One property of note for the combo box is the Style property, which can be set at design time to Drop Down combo (the default), Simple Combo, or Drop Down list. Its prefix is cbo. The combo box has all the properties and methods of the list box including the AddItem, ListCount, and List() properties.

20 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 20 More on Combo Boxes The default event for the Combo box is the Change event--to use the Click event, you must change to it. The Sorted property is a useful property for the combo and list boxes that arranges the items in the box alphabetically. Items can be removed from a combo or list box with the RemoveItem method which requires that number of the item to be remove be given. To remove a selected item, you can use the ListIndex property, eg, cboMembers.RemoveItem cboMembers.ListIndex

21 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 21 Nested Loops A Nested loop is a loop within a loop. Must complete the inner loop within the outer loop. Nested For-Next loops have a For-Next loop within a For- Next loop. The inner loop will go through all its values for each value of the outer loop. Three key programming rules to remember about using nested For-Next loops: Always use different counter variables for the outer and inner For-Next loops. Always have the Next statement for the inner For- Next loop before the Next statement for the outer For- Next loop. Always include the counter variable in the Next statements to distinguish between the loops.

22 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 22 Creating an Executable File An executable file is one that can be executed on any computer on which it is installed. Creating an executable file in VB is accomplished with the File|Make filename.exe menu selection. Once an executable file is created, a shortcut to it can be created by right- clicking the file name in Windows Explorer and selecting Create Shortcut. Drag the shortcut to the desktop.

23 Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops Sequential Access Files Combo Boxes Executable Files Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 23 Debugging Loops Debug a loop by inserting a debug.print command in the loop to print to the Immediate Window. Add a Quick Watch by locating the pointer on a variable and clicking the eyeglass icon on the Debug Toolbar. The values for this variable will be shown in the Watch Window. Use the Locals window to display the values of variables local to a procedure. Use the Toggle Breakpoint icon to pause execution at a designated line in the code and then use the various windows to view the values for variables.


Download ppt "Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops."

Similar presentations


Ads by Google