Download presentation
Presentation is loading. Please wait.
Published byCornelius Sharp Modified over 9 years ago
1
5-1 Chapter 5 The Repetition Process in VB.NET
2
5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the various types of loops that can be carried out in an OOED computer language. Discuss the statement forms used in VB.NET to handle repetition, and under-stand the use of pre- and post-test loops. Understand the concepts of variable scope and static variables.
3
5-3 Learning Objectives (continued) Use the ComboBox control to select from a list of alternatives. Understand the use of files to store data and information. Describe the creation and use of executable files in VB.NET. Use debugging tools in VB.NET to find and correct errors.
4
5-4 General Structure of a Loop Initialization : set up initial conditions Loop body : the code that will be repeated Termination condition : needed to stop
5
5-5 Types of Loops Event Driven – The repetition is determined by an event being raised Determinate – You know in advance the number of repetitions Indeterminate – You do not know the number of repetitions at the start of the loop
6
5-6 Variable Scope Form level variable : available to all code on a form (declared outside any procedure) Local or procedure level variable : available within the procedure it is declared Block level variable : available only within the block it is declared
7
5-7 Event driven loop example You enter a value in a TextBox and press Enter Then – Value gets added to a list box – The number of values gets updated – The sum of the values gets updated
8
5-8 Event driven loop user interface
9
5-9 Step-by-Step 5.1 Event-driven input Demo
10
5-10 Static Variables This is another scope level Declared inside a procedure – Static MyVariable As MyType Retains its value between execution of the procedure To make all variables in a procedure static, declare the procedure as Static – Static MyProc() …
11
5-11 Determinate Loops : For-Next The syntax: For MyVariable = MyStart To MyEnd Step MyStep ‘body of loop Next MyVariable
12
5-12 For-Next Loops: The meaning – MyVariable = MyStart is the initialization step – Body of the loop is executed for this value of MyVariable – Whenever code attains the Next MyVariable instruction MyVariable is increased by MyStep Execution jumps to top of block If MyVariable is no more than MyStart, body is executed again Otherwise, loop is complete and execution resume to first line after the loop
13
5-13 Small details In the Next instruction line, the name of the variable is optional. – If provided it must match the name of the loop variable The Step instruction is also optional – If not provided, the instruction Step 1 is assumed – i.e. by default the loop variable increases by 1
14
5-14 For-Next Loop Example Sum integers from 1 to 100 Dim intSum As Integer = 0 For i = 1 to 100 intSum = intSum + i Next i MsgBox(“The sum is “ & intSum)
15
5-15 Using the ListBox Most interaction is through its “list” of “items” – MyListBox.Items You can add items – MyListBox.Items.Add(….) You can count items – MyListBox.Items.Count You can access (read) items – MyListBox.Items(index) – Index must be between 0 and MyListBox.Items.Count -1
16
5-16 How to print to the debug window Use the WriteLine method of the Debug class – Debug.Writeline(“Here is a sweet message”) Example : show entries of a ListBox For intCounter = 0 to lstEntries.Items.Count - 1 Debug.Writeline(lstEntries.Items(intCounter)) Next
17
5-17 Step-by-Step 5-2: Sum and print using a For-Next loop Demo
18
5-18 Indeterminate Loops Four types of indeterminate loops – Looping until some condition is true with the termination condition before the body of the loop. – Looping while some condition is true with the termination condition before the body of the loop. – Looping until some condition is true with the termination condition after the body of the loop. – Looping while some condition is true with the termination condition after the body of the loop.
19
5-19 Classifying indeterminate loops Pre-test loops vs Post-test loops – Pre-test: termination condition comes before the body of loop (means that one may not go through the loop at all) – Post-test: termination condition comes after the body of loop (means that one has to go through the loop at least once) While Loops vs Until Loops – While: stops when condition becomes false – Until: stops when condition becomes true – Matter of preference: by using the negation of condition, one can transform one type into another
20
5-20 Indeterminate Loops: Syntax Pre-test Loop Do Until (or While) condition ‘body of loop Loop Post-test Loop Do Until (or While) condition ‘body of loop Loop
21
5-21 Indeterminate Loops: Examples Dim intValue As Integer = 10 Do MsgBox("Value = " & Str(intValue)) intValue = intValue - 2 Loop While intValue > 0 An equivalent Loop Until statement would be: Dim intValue As Integer = 10 Do MsgBox("Value = " & Str(intValue)) intValue = intValue - 2 Loop Until intValue <= 0
22
5-22 Data Files 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.
23
5-23 Types of data files Sequential access files – files from which the data records must be input in the same order that they were placed on the file. Direct access files (random access files) – files that can be input in any order. Database files – widely used for data storage, – must be accessed with a database management system
24
5-24 Sequential file access Three steps – Open the file it must exist – Treat the content Read and check for EOF marker to make sure you don’t go pass the end of the file – Close the file
25
5-25 Sequential File Access: Syntax Open the file – FileOpen(n, FileName,OpenMode.Input) n is a variable, literal value or expression evaluating to an integer FileName is a string: the full path to an actual file OpenMode.Input indicates the file is open for reading Close the file – FileClose(n) n represents which file to close
26
5-26 Sequential File Access: Syntax Reading the file – Input( n, list of variables) n represents which file is being read Loop through file until all done Do Until EOF(n) Input( n, MyVariable) ‘treat MyVariable Loop
27
5-27 Step-by-Step 5-3: Using an Until Loop to input data from sequential file Demo
28
5-28 Step-by-Step 5-4: Using nested loops Demo
29
5-29 Application to Vintage DVDs Allow customer to rent more than one DVDs Determine if customer is a member If not a member, enroll for future reference
30
5-30 Form for multi-DVD application
31
5-31 The ComboBox Control Combination of TextBox and ListBox Some Properties – DropDownStyle DropDown (default) Simple DropDownList – Sorted True False
32
5-32 Some methods of the Items list – Add() – RemoveAt() – Clear()
33
5-33 Step-by-Step 5-5: Handling multiple DVD rentals Demo
34
5-34 Using Autos and Locals windows Autos window – Displays values of variables in current statement plus three statements on either side Locals window – Displays values of all variables that are local to the current procedure
35
5-35 Adding a watch on a variable When in break mode, position cursor on a variable Select the Quick Watch menu option from Debug menu Select the Add Watch menu option from Debug menu
36
5-36 Copyright 2004 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.