View
224
Download
0
Tags:
Embed Size (px)
Citation preview
BACS 287
BACS 287
Programming Fundamentals 4
BACS 287
Programming Fundamentals
This lecture introduces the following iteration control structure topics:– Do Loops– For-Next Loops– For Each-Next
BACS 287
Loop Structures
Visual Basic provides 3 major loop structures.– Do-Loop (Do-While, Do-Until)– For-Next– For Each-Next
There are 4 variations of the Do-Loop. The For Each-Next loop only applies to
objects in a collection and arrays.
BACS 287
Do-While Loop Structures
There are 2 “While” structures available:1) Do While condition test at top of loop
statements[Exit Do]
Loop
2) Dostatements[Exit Do]
Loop While condition test at bottom of loop
BACS 287
Do-While Loop Example 1
Dim blnFlag as boolean = FalseDo While blnFlag = True Debug.Writeline “Hi Mom”Loop
How many times will this execute?
BACS 287
Do-While Loop Example 2
Dim blnFlag as boolean = FalseDo Debug.Writeline “Hi Mom”Loop While blnFlag = True
How many times will this execute?
BACS 287
Do-Until Loop Structures
There are 2 “Until” structures available:1) Do Until condition test at top of loop
statements[Exit Do]
Loop
2) Dostatements[Exit Do]
Loop Until condition test at bottom of loop
BACS 287
Do-Until Loop Example 1
Dim shoNum as short = 7Dim shoGuess as short = 0shoGuess = InputBox(“Enter Guess”)Do Until shoGuess = shoNum
shoGuess = InputBox(Enter Guess”)Loop
What happens when this executes?
BACS 287
Do-Until Loop Example 2
Dim shoNum as short = 7Dim shoGuess as short = 0shoGuess = InputBox(“Enter Guess”)Do
shoGuess = InputBox(Enter Guess”)Loop Until shoGuess = shoNum
What happens when this executes?
BACS 287
For-Next Loops
For-Next loops let you execute a block of statements a fixed number of times.
For counter = start To end [Step increment]
statements
[Exit For]
Next [counter]
BACS 287
For-Next Loops – Alternate Syntax
A newer syntax exists for For-Next loops that does not require you to define the counter variable prior to the loop. This utilizes new block structure scope for variables.
For counter [AS datatype] = start To end [Step increment]
statements
[Exit For]
Next [counter]
BACS 287
For-Next Example 1
Dim shoCnt as ShortFor shoCnt = 0 to 9
Debug.Writeline shoCntNext shoCnt
OR
For shoCnt as short = 0 to 9Debug.Writeline shoCnt
Next shoCnt
BACS 287
Nested Loops
Dim Check As Boolean = True Dim Counter As Integer = 0 Do ' Outer loop
Do While Counter < 20 ' Inner loop Counter Counter += 1 ' Increment Counter If Counter = 10 Then
Check = FalseExit Do ' Exit inner loop
End If Loop ' Exit inner loop
Loop Until Check = False ' Exit outer loop
BACS 287
For-Next Example 2
For shoRow as short = 0 to 9For shoCol as short = 0 to 4
Debug.Writeline shoRow; shoColNext shoCol
Next shoRow
BACS 287
For-Next Loops
For-Next loops are commonly used to increment subscripts to process the elements of an array.
If you have a multi-dimension array, use nested For-Next loops.
When nested, the outer For-Next increments slowest and the inner most For-Next increments fastest.
BACS 287
For-Next Example 3
Dim strName(9,4) as String
...load the array
...For shoRow as integer = 0 to 9
For shoCol as integer = 0 to 4Debug.Writeline strName (shoRow, shoCol)
Next shoColNext shoRow
BACS 287
For Each-Next Structure
For Each-Next loops are similar to For-Next loops except that it repeats a block of statements for each element of a collection of objects or elements in an array.
This is useful when you do not know how many elements there are in a collection or array.
BACS 287
For Each-Next Structure
For Each element In group
statements
[Exit For]
Next [element]
If you are using this on a multi-dimension array, you do not need to nest For Each statements to access all elements.
BACS 287
For Each-Next Structure - Alternate Syntax
For Each element [AS datatype] In group
statements
[Exit For]
Next [element]
The ‘datatype’ must be the type of object that is present in the collection. For example, if the group is a collection of strings, then the datatype would be ‘string’.
BACS 287
For Each-Next Example 1
Dim Found As Boolean = False Dim objCollection As ObjectFor Each objObject as object In objCollection
If CStr(objObject.Text)= "Hello" Then Found = True Exit For
End If Next
BACS 287
For Each-Next Example 2
Dim intX (9, 19) as integerDim objCnt as object...For Each objCnt In intX initializes matrix
intX = 5Next objCnt...For Each objCnt In intX prints matrix
Print.Writeline objCntNext objCnt
BACS 287
In-Class Loop Example 1
Use a Do-While loop that tests at the top to sum the integers 1 to 100. Print the sum after you have computed it.
BACS 287
Answer Example 1
Dim shoSum as short = 0Dim shoCnt as short = 1Do While shoCnt < 101
shoSum += shoCntshoCnt += 1
LoopDebug.Writeline shoSum
BACS 287
In-Class Loop Example 2
Use a Do-Until loop that tests at the bottom to ask the user for a password until their answer is the same as a public variable named txtPassword. Assume that txtPassword is already defined and assigned elsewhere. Print “success” when they give the correct answer.
BACS 287
Answer Example 2
Dim strAnswer As String
Do strAnswer = InputBox("Enter the Password")Loop Until strAnswer = strPassword
Debug.Writeline "success"
BACS 287
In-Class Loop Example 3
Modify example case #2 to give the user 3 tries to get the correct password. After the 3rd try, tell them “sorry”.
BACS 287
Answer Example 3
Dim strAnswer As StringDim shoCnt As Short = 1
Do strAnswer = InputBox("Enter the Password") shoCnt = shoCnt + 1Loop Until strAnswer = strPassword Or shoCnt = 4
If strAnswer = strPassword Then Debug.Writeline "success"Else Debug.Writeline "sorry"End If