Tutorial 6 The Repetition Structure

Slides:



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

Chapter 6: The Repetition Structure
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.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 7: Sub and Function Procedures
1.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
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.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
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.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
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.
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.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
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.
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.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
1.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
Controlling Program Flow with Looping Structures
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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()
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
© 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,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Visual Basic Fundamental Concepts
Microsoft Visual Basic 2008: Reloaded Third Edition
UNIT 5 Lesson 15 Looping.
Clearly Visual Basic: Programming with Visual Basic nd Edition
Visual Basic 6 (VB6) Data Types, And Operators
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Lists, Loops, Validation, and More
Repeating Program Instructions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Visual Basic..
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter (3) - Looping Questions.
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

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 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 Tutorial 6: 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 Tutorial 6: The Repetition Structure

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) Tutorial 6: The Repetition Structure

A For … Next Example Dim intCount As Integer For intCount = 0 to 3 Step 1 Debug.WriteLine(intCount) Next intCount For intCount = 3 to 0 Step -1 For intCount = 0 to 10 Step 2 Dim sngLoc As Single For sngLoc = 0.5 To 15 Step 0.5 Debug.WriteLine(sngLoc) Next sngLoc Tutorial 6: The Repetition Structure

Flowchart and Pseudocode Hexagon Repeat for intCount = 1 to 3 by 1 Display intCount Next Iteration intCount > 3 +=1 1 Display intCount Tutorial 6: The Repetition Structure

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 Pretest condition Do While Condition [loop instructions] Loop Do Until Condition Posttest condition Do Loop While Condition Loop Until Condition Tutorial 6: The Repetition Structure

Loop Examples Dim intCount As Integer = 1 Do While intCount < 3 Debug.WriteLine(intCount) intCount += 1 Loop Do Until intCount > 3 Do Loop While intCount < 3 Loop Until intCount > 3 Tutorial 6: The Repetition Structure

Do While Pretest Loop intCount = 1 intCount <= 3 F T Repeat while intCount < 3 Display intCount Add 1 to intCount End Repeat intCount <= 3 F T Display intCount intCount += 1 Tutorial 6: The Repetition Structure

Do Until Posttest Loop intCount = 1 Display intCount intCount += 1 Repeat Display intCount Add 1 to intCount End Repeat until intCount > 3 Display intCount intCount += 1 intCount > 3 T F Tutorial 6: The Repetition Structure

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 Tutorial 6: The Repetition Structure

Using Collections Lesson 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 Tutorial 6: The Repetition Structure

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 Identifies by an index, automatically assigned by Visual Basic .NET when object is created Refer to a control Controls.Item(index) Controls are numbered LIFO – that is, the last control object has an index = 0 The value of Controls.Count gives the number of controls on a form Tutorial 6: The Repetition Structure

Accessing the Controls Collection Dim intX As Integer = 0 For intX = 0 To Controls.Count – 1 Debug.WriteLine(Controls(intX).Name) Next intX Do While intX < Controls.Count If TypeOf Controls.Item(intX) Is TextBox Then Controls.Item(intX).Text = “” End If intX += 1 Loop Tutorial 6: The Repetition Structure

Object Variables An object variable is a memory location that can store the address of an object 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 assign an object variable objStateTextBox = Me.StateTextBox Tutorial 6: The Repetition Structure

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 For Each element In group [processing statements for element] Exit For Next element Dim objTextBox As TextBox For Each objTextBox In Me.Controls If objTextBox.Text = “Hello” Then End If Next Tutorial 6: The Repetition Structure

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 stop Is control a label? T Remove the border F Tutorial 6: The Repetition Structure

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 collectionName.Remove(index|key) Tutorial 6: The Repetition Structure

Creating a User-Defined Collection 'declare form-level collections Private mCheckBoxCollection As New Collection() Private Sub GradeForm_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim intCtr As Integer For intCtr = 0 To Controls.Count - 1 If TypeOf (Controls(intCtr)) Is CheckBox Then mCheckBoxCollection.Add(Controls(intCtr)) End If Next … Tutorial 6: The Repetition Structure

Parallel Collection 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 For intCtr = 0 To Controls.Count - 1 If TypeOf (Controls(intCtr)) Is TextBox Then mTextBoxCollection.Add(Controls(intCtr), _ Replace(Controls(intCtr).Name, "TextBox", _ "CheckBox")) End If Next Tutorial 6: The Repetition Structure

The Enabled Property Private Sub ProcessCheckBox(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Proj1CheckBox.Click, … Dim objCB As CheckBox, objTB As TextBox objCB = sender ' assign sender to the object variable objTB = mTextBoxCollection(oCB.Name) If objCB.Checked Then objTB.Enabled = True objTB.Focus() Else objTB.Text = "" objTB.Enabled = False End If End Sub Tutorial 6: The Repetition Structure

Completing the Grade Calculator Application Lesson 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 Tutorial 6: The Repetition Structure

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 Tutorial 6: The Repetition Structure

Tutorial 6: The Repetition Structure

Coding the GradeForm’s Closing 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 Tutorial 6: The Repetition Structure