36
5-1 Chapter 5 The Repetition Process in VB .NET

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

Embed Size (px)

Citation preview

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 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.

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.

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

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

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

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

5-8

Event driven loop user interface

5-9

Step-by-Step 5.1Event-driven input

Demo

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() …

5-11

Determinate Loops : For-Next

The syntax:

For MyVariable = MyStart To MyEnd Step MyStep

‘body of loop

Next MyVariable

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

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

5-14

For-Next Loop Example

Sum integers from 1 to 100

Dim intSum As Integer = 0

For i = 1 to 100intSum = intSum + i

Next i

MsgBox(“The sum is “ & intSum)

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

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 ListBoxFor intCounter = 0 to lstEntries.Items.Count - 1

Debug.Writeline(lstEntries.Items(intCounter))

Next

5-17

Step-by-Step 5-2: Sum and print using a For-Next loop

Demo

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.

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

5-20

Indeterminate Loops: Syntax

Pre-test LoopDo Until (or While) condition

‘body of loop

Loop

Post-test LoopDo Until (or While) condition

‘body of loop

Loop

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

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.

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

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

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

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 doneDo Until EOF(n)

Input( n, MyVariable)‘treat MyVariable

Loop

5-27

Step-by-Step 5-3: Using an Until Loop to input data from sequential file

Demo

5-28

Step-by-Step 5-4: Using nested loops

Demo

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

5-30

Form for multi-DVD application

5-31

The ComboBox Control

Combination of TextBox and ListBox Some Properties

– DropDownStyle DropDown (default) Simple DropDownList

– Sorted True False

5-32

Some methods of the Items list

– Add()– RemoveAt()– Clear()

5-33

Step-by-Step 5-5: Handling multiple DVD rentals

Demo

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

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

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