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.

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Repeating Actions While and For Loops
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Programming Based on Events
List-based Controls. Slide 2 Introduction There are several controls that work with lists ComboBox ListBox CheckedListBox.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
The Repetition Process in Visual Basic. The Repetition Process The capability to repeat one or more statements as many times as necessary is what really.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
1.
CS 101 Test 2 Study Guide Acronyms RAD - Rapid Application Development IDE - Integrated Development Environment GUI - Graphical User Interface VB - Visual.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Unit 6 Repetition Processing Instructor: Brent Presley.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Microsoft Visual Basic 2008: Reloaded Third Edition
A variable is a name for a value stored in memory.
UNIT 5 Lesson 15 Looping.
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
3rd prep. – 2nd Term MOE Book Questions.
Introducing Do While & Do Until Loops & Repetition Statements
Repeating Program Instructions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter (3) - Looping Questions.
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Introduction to Computer Programming IT-104
Presentation transcript:

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

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

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.

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.

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

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

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

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

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.

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.

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.

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.

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.

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)

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.

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

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

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

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.

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.

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

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