270 likes | 432 Views
BACS 287. Programming Fundamentals 4. Programming Fundamentals. This lecture introduces the following iteration control structure topics: Do Loops For-Next Loops For Each-Next. Loop Structures. Visual Basic provides 3 major loop structures. Do-Loop (Do-While, Do-Until) For-Next
E N D
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) Do statements [Exit Do] Loop While condition test at bottom of loop BACS 287
Do-While Loop Example 1 Dim blnFlag as boolean = False Do 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 = False Do 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) Do statements [Exit Do] Loop Until condition test at bottom of loop BACS 287
Do-Until Loop Example 1 Dim shoNum as short = 7 Dim shoGuess as short = 0 shoGuess = 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 = 7 Dim shoGuess as short = 0 shoGuess = 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] = startTo end [Step increment] statements [Exit For] Next [counter] BACS 287
For-Next Example 1 Dim shoCnt as Short For shoCnt = 0 to 9 Debug.Writeline shoCnt Next shoCnt OR For shoCnt as short = 0 to 9 Debug.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 = False Exit 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 9 For shoCol as short = 0 to 4 Debug.Writeline shoRow; shoCol Next 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 4 Debug.WritelinestrName (shoRow, shoCol) Next shoCol Next 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 Object For 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 integer Dim objCnt as object ... For Each objCnt In intX initializes matrix intX = 5 Next objCnt ... For Each objCnt In intX prints matrix Print.Writeline objCnt Next 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 = 0 Dim shoCnt as short = 1 Do While shoCnt < 101 shoSum += shoCnt shoCnt += 1 Loop Debug.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 String Dim shoCnt As Short = 1 Do strAnswer = InputBox("Enter the Password") shoCnt = shoCnt + 1 Loop Until strAnswer = strPassword Or shoCnt = 4 If strAnswer = strPassword Then Debug.Writeline "success" Else Debug.Writeline "sorry" End If BACS 287