looping structures do loops, for next do...loop while structures check the condition after executing...
TRANSCRIPT
![Page 1: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/1.jpg)
![Page 2: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/2.jpg)
Looping Structures
Do Loops, For Next
![Page 3: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/3.jpg)
Do...Loop While structures check the condition after executing the code and repeat a code block until the test expression evaluates as false.
DostrPassword = InputBox("Enter your
password")Loop While strPassword <> "Lefty"
The example code will loop until the correct password is entered
![Page 4: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/4.jpg)
Do...Loop Until structures check the condition after executing the code and repeat a code block until the test expression evaluates as true.
Dim intLoopCount As Integer
Do intLoopCount = intLoopCount + 1Loop Until MsgBox("Loop?", vbYesNo) = vbNo
The example code will loop until the No button is clicked in a message box.
![Page 5: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/5.jpg)
Do While...Loop structures check the condition beforeexecuting the code. The code in the loop is executed only if the condition evaluates as true, and repeats until the test expression evaluates as false.
Dim strPasswordTry As StringConst PASSWORD As String = "password"
Do While strPasswordTry <> PASSWORD strPasswordTry = InputBox("Enter password")Loop
The example code will ask the user for a password until the correct password is typed into the input box
![Page 6: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/6.jpg)
Do Until...Loop structures check the condition before executing the code. The code in the loop is executed only if the condition evaluates as false, and repeats until the test expression evaluates as true.
Dim intValue As IntegerDim strInput As String
Do Until intValue = 3 strInput = InputBox("Pick a number between 1 and 5") intValue = Val(strInput)Loop
![Page 7: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/7.jpg)
CHALLENGE:
Dim strMsg As StringDim strFilespec As StringDim strMatch As String
strMsg = "Enter a file specification."'Get file extensionstrFilespec = InputBox(strMsg)'Find first matchstrMatch = Dir(strFilespec)Do Until Len(strMatch) = 0 'Display matching files MsgBox strMatch 'Find next match strMatch = Dir()LoopMsgBox "There are no more matching files."
![Page 8: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/8.jpg)
When both the condition and the number of times a code
block will execute is known, use a For loop.
For counter = start To end [Step increment]
[statements]
Next [counter]
![Page 9: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/9.jpg)
Dim intNumStudents As IntegerDim intCounter As IntegerDim sglScore As SingleDim sglTotalScore As SingleDim sglAverage As Single
intNumStudents = InputBox(prompt:="How many students?") For intCounter = 1 To intNumStudents sglScore = CSng(Val(InputBox(prompt:="Enter score"))) sglTotalScore = sglTotalScore + sglScoreNext intCounter sglAverage = sglTotalScore / intNumStudents
The following example code displays an input box that prompts the user to enter a number. It then executes
the code in the For...Next loop the specified number of times, calculating and displaying a result.
![Page 10: Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test](https://reader036.vdocuments.mx/reader036/viewer/2022082713/5697bfea1a28abf838cb7669/html5/thumbnails/10.jpg)
Dim intOuter As Integer, intInner As Integer Dim strOut As String ' Outer Loop For intOuter = 1 To 10 ' Inner Loop For intInner = 1 To 10 ' Concatenate Outer * Inner results to strOut strOut = strOut & " " & Format(intOuter * intInner, "@@@") Next intInner ' Add a carriage return and linefeed to end of line strOut = strOut & vbCrLf Next intOuter ' Monospaced font Me.Font = "Courier New" ' Display strOut on form Me.Print strOut
The first For...Next loop generates each new line within the table. The second
For...Next loop is nested within the first and generates each column within a row