1 / 23

Using Looping Structures and Lists

Using Looping Structures and Lists. Chapter 4. For … Next Statements Repeat a specific number of times. For intCounter = 1 To 5 Statement block Next intCounter intCounter values are 0,[1, 2, 3, 4, 5], 6. intCounter = 0. intCounter = 6.

kato-haley
Download Presentation

Using Looping Structures and Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Looping Structures and Lists Chapter 4

  2. For … Next Statements Repeat a specific number of times • For intCounter = 1 To 5 • Statement block • Next intCounter • intCounter values are 0,[1, 2, 3, 4, 5], 6 intCounter = 0 intCounter = 6

  3. For … Next Statements Repeat a specific number of times • For intCounter = 5 To 1 Step -1 • Statement block • Next intCounter • intCounter values are 0,[5, 4, 3, 2, 1], 0

  4. For … Next Statements Repeat a specific number of times • Counter must be a numeric variable, usually integer. • Startvalue, Endvalue, and Stepvalue are numeric, usually integer, can be constants or variables. • Startvalue, Endvalue can be positive, negative or zero. • Stepvalue can be positive or negative but not zero. • Stepvalue defaults to 1 is omitted.

  5. For … Next Example • Write a For Next loop where the user inputs the names of 4 players on a bowling team and displays them on different lines in a multi-line text box called txtPlayers.

  6. For … Next Example • For intCounter = 1 To 4 • strName = strName & InputBox(“Type a name for player “ & intCounter) & vbCrLf • Next intCounter • txtPlayers.Text = strName

  7. For … Next Example • intNbrOnTeam = Val(InputBox(“How many people are on the team?”)) • For intCounter = 1 To intNbrOnTeam • strName = strName & InputBox(“Type a name for player “ & intCounter) & vbCrLf • Next intCounter • txtPlayers.Text = strName

  8. Debug Technique • intNbrOnTeam = Val(InputBox(“How many people are on the team?”)) • For intCounter = 1 To intNbrOnTeam • strName = strName & InputBox(“Type a name for player “ & intCounter) & vbCrLf • Debug.Writeline(intCounter) • Debug.Writeline(strName) • Next intCounter • txtPlayers.Text = strName

  9. Create a List of Interest Rates • sngBegRate = Val(InputBox(“What is the beginning interest rate?”)) • sngEndRate = Val(InputBox(“What is the ending interest rate?”)) • For sngRate = sngBegRate To sngEndRate Step .05 • strRates = strRates & format(sngRate,”Percent”) & vbCrLf • Next sngCounter • txtRates.Text = strRates

  10. Endless Loops • A loop which will never reach the end value is an endless, or infinite, loop. • This is the worst error you can make in coding. • Changing the Counter value inside the loop can produce an endless loop. • To exit an endless loop, press the ctrl and break keys together.

  11. Incrementing Variables intCounter = IntCounter + 1 intCounter += 1 Each takes the value of integer counter, adds one to it, and stores the result in integer counter.

  12. Working with Lists Use a • ComboBox when the user needs to type in their own selection, this creates a possibility of input errors. • ListBox – when the user is limited to the specific choices listed, no possibility of input errors.

  13. ListBox Properties • Items – String Collection Editor • Index – points to a particular item in the list, indexes begin with 0. • SelectionMode • None – no items may be selected. • One – only one item may be selected at a time. • MultiSimple – more than one item may be selected. • MultiExtended – more than one item may be selected.

  14. Items.Add method • Adds items to the collection during run time. • Used for ComboBoxes and ListBoxes. • Listboxname.Items.Add value • Lists are usually populated in the Form load procedure.

  15. Adding items to a list at run time. • Listboxname.Items.Add value • Numeric values can be added in a loop • For sngRate = .05 To .12 Step .01 • lstRates.Items.Add sngRate • Next sngRate • lstRates.SetSelected = (2, True)

  16. ListBox.SetSelected method • listBox.SetSelected (index, value) • The index refers to which item is selected. • The selected value is either true or false. • lstRates.SetSelected = (2, True) • Selects the third item in the list as the default.

  17. Determine the selected item • A listbox used the SelectedIndex property to determine which item is selected. • This only works for single select list boxes.

  18. Which rate is selected? • Select Case lstRates.SelectedIndex • Case 0 • Case 1 • Case 2 • Case 3 • End Select

  19. Display Payments in a List Box • lstRates.Clear ‘first clear the list • For intCtr = 1 To intTerm * 12 • lstPayments.Items.Add (“Payment #” & intCtr & vbTab & Format(Pmt(sngRate/12, intTerm * 12, -decPrincipal), “Currency”) • Next

  20. For…Each…Next Statement • This statement allows us to work with each item in a collection. • A collection is a group of individual objects treated as one unit. • Items in a ComboBox or ListBox are one kind of collection. • Other collections are in Chapter 8.

  21. For…Each…Next Statement • Executes the statements in the loop for each element in a collection. • For Each element In collection • Statements • Next [element] • Element is an object variable which keeps track of each item in a collection.

  22. For…Each…Next Statement • For Each objControl In Controls • If TypeOf objControl IS TextBox Then • objTextBox = objControl • If objTextBox.Visible Then • objTextBox.Visible = False • Else • objTextBox.Visible = True • End If • End If • Next objControl

  23. List Folders on a Disk Drive • Dim strLetter As String • Dim folders() As DirectoryInfo • lstDir.Items.Clear() • strLetter = InputBox(“Enter a drive letter.”) • Folders = New DirectoryInfo(strLetter & “:\”) • Dim folder as DirectoryInfo • For Each folder In folders • lstDir.Items.Add(folder.FullName) • Next

More Related