Visual Basic 6
Grundläggande syntax
Sök…
if / annars uttalande
If condition Then
code to execute if true
ElseIf condition Then
code
Else
code to execute if conditions are both false
End If
för slinga
For I as Integer = 1 To 10 Step 1
code to execute
Next
Steg är valfritt och steg 1 är standard. Steg berättar för hur det ska räkna, så -1 skulle få det att subtrahera 1 varje gång och steg 5 skulle få det att lägga till 5 varje gång genom slingan.
Om slingan behöver stoppas, kan Exit For
uttalande användas, som i exemplet nedan;
Dim iIndex as integer
For I as Integer = 1 To 10 Step 1
Debug.Print I
iIndex = I * 10
If iIndex > 90 Then
Exit For
End If
Loop
Här, istället för att skriva ut 1 till 10, kommer det att stanna vid 9, eftersom villkoret berättade för processen att stoppa när iIndex når 90.
Gör slingan
En annan vanlig typ av slinga i Visual Basic är DO loop
, som skulle köra ett kodstycke kontinuerligt tills det får höra att stoppa. Tvärtom från vissa andra slingor där index används för att stoppa processen, i denna speciella slinga, bör det sägas att stoppa.
Ett enkelt exempel som illustrerar slingan är som följer
Dim iIndex1 As Integer
iIndex1 = 1
Do
Debug.Print iIndex1
iIndex1 = iIndex1 + 1
If iIndex1 = 10 Then
Exit Do
End If
Loop
Ovanstående koddel kommer att ta ett index, initialiserat till 1 och öka det. En Debug.Print
hjälper till att skriva ut indexet för att racka öglan. På varje slinga kommer koden att verifiera om indexet har nått 10 och om och bara om villkoret är sant kommer Exit Do
att köras, vilket stoppar slingan.
Välj ärendeuttalande
Dim number As Integer = 8
Select Case number
Case 1 To 5
Debug.WriteLine("Between 1 and 5, inclusive")
' The following is the only Case clause that evaluates to True.
Case 6, 7, 8
Debug.WriteLine("Between 6 and 8, inclusive")
Case 9 To 10
Debug.WriteLine("Equal to 9 or 10")
Case Else
Debug.WriteLine("Not between 1 and 10, inclusive")
End Select