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

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Using Macros and Visual Basic for Applications (VBA) with Excel
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
VB Default Controls List Box, Combo Box
Chapter 5: Loops and Files.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
Copyright © 2001 by Wiley. All rights reserved. Chapter 8: Security, Menus, and Files Security Menu Systems Creating a Memo Editor Using the Clipboard.
5.05 Apply Looping Structures
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Copyright © 2001 by Wiley. All rights reserved. Chapter 10: Advanced Database Operations Revising Vintage Videos Setting RecordSource at run time DBGrid.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
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 4: The Selection Process in Visual Basic.
Chapter 12: How Long Can This Go On?
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
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.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
© 1999, by Que Education and Training, Appendix A, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Introduction to Problem Solving and Control Statements.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 7 - Lists, loops and printing w List boxes and combo boxes several types can add items at design time or during run time user select from predefined.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Unit 6 Repetition Processing Instructor: Brent Presley.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© 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.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Introduction to Computer Programming IT-104
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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.

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

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

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.

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

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.

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.

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.