tutorial 6 the repetition structure n.
Skip this Video
Loading SlideShow in 5 Seconds..
Tutorial 6 The Repetition Structure PowerPoint Presentation
Download Presentation
Tutorial 6 The Repetition Structure

Loading in 2 Seconds...

play fullscreen
1 / 29

Tutorial 6 The Repetition Structure - PowerPoint PPT Presentation

  • Uploaded on

Tutorial 6 The Repetition Structure. The Repetition Structure (Looping) Lesson A Objectives. After completing this lesson, you will be able to: Code the repetition structure using the For…Next and Do…Loop statements Write pseudocode for the repetition structure

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Tutorial 6 The Repetition Structure' - minowa

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the repetition structure looping lesson a objectives
The Repetition Structure (Looping)Lesson A Objectives

After completing this lesson, you will be able to:

  • Code the repetition structure using the For…Next and Do…Loop statements
  • Write pseudocode for the repetition structure
  • Create a flowchart for the repetition structure
  • Display a message in the Output window while an application is running
  • Change the location and size of a control while an application is running
  • Initialize and update counters and accumulators
the repetition structure
The Repetition Structure
  • Most programs also contain the selection structure, which you learned about in Tutorials 4 and 5
  • Programmers use the repetition structure, referred to more simply as a loop, when they need the computer to repeatedly process one or more program instructions until some condition is met, at which time the loop ends
  • In a pretest loop, the evaluation occurs before the instructions within the loop are processed
  • In a posttest loop, the evaluation occurs after the instructions within the loop are processed
the for next loop
The For … Next Loop
  • You can use the For…Next statement to code a loop whose instructions you want processed a precise number of times
  • Syntax:

For counter = startValue To endValue [Step stepValue]

[instructions you want repeated]

Next [counter]

  • counter is the name of a numeric variable and it keeps track of how many times the loop instructions are repeated
  • startValue, endValue, and stepValue must be numeric and they can be either positive or negative, integer or non-integer (default stepValue is 1)
flowchart and pseudocode

Repeat for intCount = 1 to 3 by 1

Display intCount

Next Iteration

Flowchart and Pseudocode


> 3



Display intCount

the do loop statement
The Do…Loop Statement
  • Unlike the For…Next statement, the Do…Loop statement can be used to
  • Code both a pretest loop and a posttest loop
  • The Do…Loop statement begins with the Do clause and ends with the
  • Loop clause
do while pretest loop
intCount = 1

Repeat while intCount < 3

Display intCount

Add 1 to intCount

End Repeat

Do While Pretest Loop

intCount = 1

intCount <= 3



Display intCount

intCount += 1

do until posttest loop
intCount = 1


Display intCount

Add 1 to intCount

End Repeat until intCount > 3

Do Until Posttest Loop

intCount = 1

Display intCount

intCount += 1

intCount > 3



using counters and accumulators
Counters and accumulators are used within a repetition structure to calculate subtotals, totals, and averages

Initialized (usually to 0 or 1) outside the loop and updated within the loop

A counter is a numeric variable used for counting something and is typically updated by 1

An accumulator is a numeric variable used for accumulating (adding together) and is updated by an amount that varies

Initializing means to assign a beginning value to the counter or accumulator

Updating, also called incrementing, means adding a number to the value stored in the counter or accumulator

Using Counters and Accumulators
using collections lesson b objectives
Using CollectionsLesson B Objectives

After completing this lesson, you will be able to:

  • Access the controls in the Controls collection
  • Code the repetition structure using the For Each…Next statement
  • Create an object variable
  • Create a collection
  • Create parallel collections
  • Enable and disable a control
the controls collection
The Controls Collection
  • The controls contained on a Windows form belong to the Controls collection in Visual Basic .NET
  • A collection is simply a group of one or more individual objects treated as one unit
  • Identified by an index, automatically assigned by Visual Basic .NET when object is created
  • Referring to a control: Controls.Item(index) or Controls(index), i.e., the word Item is optional
  • Controls are numbered in reverse – that is, the last control object created has an index = 0
  • The value of Controls.Count gives the number of controls on a form. Thus, they are indexed 0 to Count-1
object variables
Object Variables
  • An object variable is a memory location that can store the address of an object, i.e. a pointer
  • The address indicates where the object is located in the computer’s internal memory
  • An object variable is initialized to the keyword Nothing, which simply means that the object variable does not currently contain an address
  • You can assign an object variable a value, e.g.,

objStateTextBox = StateTextBox

(Technically this puts the address of StateTextBox into objStateTextBox)

the for each next statement
The For Each…Next Statement
  • The For Each…Next statement is used to code a loop whose instructions you want processed for each object in a collection
flowchart and pseudocode for the for each next
Flowchart and Pseudocode for the For Each … Next

Repeat for each Control in Controls collection

Repeat for each Control in Collection

if control is a label

remove border

end if

end repeat


Is control a label?


Remove the border


creating a user defined collection
Creating a User-Defined Collection
  • A user-defined collection allows you to group related controls together
  • To define a collection

Dim collectionName As New Collection()

  • To insert an object into the collection

collectionName.Add(object[, key])

  • To access an object in the collection:

objMyObject = collectionName(index)

objMyObject = collectionName(key)

  • To remove an object from the collection


  • NOTE: User-defined collections index from 1 not 0
accessing a user defined collection
Accessing a User-Defined Collection
  • Referring to example 2 on the last slide, to refer to the second label in MyCollection (the TotalSalesLabel ) and assign it a value of 10000 we can do:

MyCollection(2).Text = 10000 or

MyCollection.Item(2).Text = 10000 or

MyCollection(“Total).Text = 10000 or

MyCollection.Item(“Total”) = 10000

parallel collections
Parallel Collections
  • Collections whose objects are related in some way are called parallel collections
  • You can indicate to the computer that two collections are parallel collections by setting the key argument for each object in one of the collections to the name of the corresponding object in the other collection
referencing an object by its key
Referencing an object by its key
  • Dim TextBoxCollection as New Collection()
  • Dim LabelCollection as New Collection()
  • TextBoxCollection.Add(NameTextBox)
  • TextBoxCollection.Add(AddressTextBox)
  • LabelCollection.Add(NameLabel, “NameTextBox”)
  • LabelCollection.Add(AddressLabel, “AddressLabel”)
  • LabelCollection(1)
completing the grade calculator application lesson c objectives
Completing the Grade Calculator ApplicationLesson C Objectives

After completing this lesson, you will be able to:

  • Select the existing text when the user tabs to a text box
  • Prevent a form from closing
coding the displaybutton s click event procedure
Coding the DisplayButton’s Click Event Procedure
  • You still need to code the DisplayButton’s Click event procedure, the GradeForm’s Closing event procedure, and the Enter event procedure for the text boxes
  • You begin with the DisplayButton’s Click event procedure as the pseudocode in Figure 6-50 illustrates
coding the gradeform s closing event procedure
Coding the GradeForm’sClosing Event Procedure
  • A form’s Closing event occurs when a form is about to be closed
  • You can close a form using either the Close button on its title bar, or the Me.Close( ) statement in code