Loops in Visual Basic

By Herbert J. Bernstein

© Copyright 1999 Herbert J. Bernstein

 

There are six major loop control structures in Visual Basic

The first two forms have the same meaning. The use of Until has the opposite meaning to the use of While. When While is used, the loop executes if the expression is true. When Until is used the loop executes is the expression is false. Putting the Until or While at the end of the loop causes the loop statement(s) to be executed before the test is made on the condition.

The For...Next loop for positive steps is equivalent to

variable = expression_1

While ( variable <= expression_2 )
statements(s)

variable = variable + expression_3
Wend

If the increment given in expression_3 is 1, the Step expression_3 may be dropped.

Thus we may modify the example given in converting strings of digits to numbers by changing the While…Wend loop to a For…Next loop

 

Private Function condexp(condition, exp1, exp2) As Variant
  If (condition) Then
    condexp = exp1
  Else
    condexp = exp2
  End If
End Function

Private Function myatod(xstring$) As Double

  Dim digfound As Integer  ' flag for digit found
  Dim isign As Integer     ' flag for sign
                           '   0 = not found, 1 = '+', -1 = '-'
  Dim decpos As Integer    ' position after the decimal point
  Dim xpow10 As Double     ' next (negative) power of ten
  Dim ipos As Integer      ' position within string
  Dim idig As Integer      ' current numeric digit
  Dim char as String       ' next character from string

  digfound = 0
  isign = 0
  decpos = 0
  xpow10 = 1.#
  Rem ipos = 0
  myatod = 0.#
 
  Rem While (ipos < Len(xstring))
  Rem ipos = ipos + 1
  For ipos = 1 To Len(xstring)
    c = mid$(xstring,ipos,1)
    Select Case (c) 
      Case "0"
         idig = 0
         digfound = 1
      Case "1"
         idig = 1
         digfound = 1
      Case "2"
         idig = 2
         digfound = 1
      Case "3"
         idig = 3
         digfound = 1
      Case "4"
         idig = 4
         digfound = 1
      Case "5"
         idig = 5
         digfound = 1
      Case "6"
         idig = 6
         digfound = 1
      Case "7"
         idig = 7
         digfound = 1
      Case "8"
         idig = 8
         digfound = 1
      Case "9"
         idig = 9
         digfound = 1
      Case "."
         If ( decpos = 0 ) Then
          idig = 0
          digfound = -1
          decpos = decpos+1
          xpow10 = xpow10/10.#
        Else
          If ( isign <>0 ) Then
            myatod = CDbl(isign)*myatod
            Exit Function
          EndIf
        EndIf
      Case "-", "+"
        If (isign = 0 And digfound = 0) Then
          isign = condexp( (c = "-") , -1, 1)
        Else
          If (isign <> 0 ) Then
            myatod = CDbl(isign)*myatod
          End If
          Exit Function
        EndIf
      Case Else
         idig = -1
    End Select    
    If (idig = -1) Then
      If (isign <> 0 ) Then
        myatod = CDbl(isign)*myatod
      EndIf
      Exit Function
    Else
      If (digfound > 0) Then
        If ( decpos = 0 ) Then
          myatod = myatod*10.# + CDbl(idig)
        Else
          myatod = myatod + CDbl(idig)*xpow10
          decpos = decpos +1
          xpow10 = xpow10/10.#
        End If
      End If
    End If
  Rem Wend
  Next ipos
  If (isign <> 0 ) Then
    myatod = CDbl(isign)*myatod
  End If
End Function

Private Sub Command1_Click()
  Picture1.Cls
  Picture1.Print myatod(Text1.text)
End Sub