For...Next lause

Kordab mĂ€Ă€ratud arv kordi ploki For...Next vahel olevaid lauseid.

SĂŒntaks:

``````
For counter=start To end [Step step]
lause sisu
[Exit For]
lause sisu
Next [counter]
``````

``````
For Each item In list
lause sisu
[Exit For]
lause sisu
Next [item]
``````

counter: Loop counter initially assigned the value to the right of the equal sign (start). Only numeric variables are valid. The loop counter increases or decreases according to the variable step until end is passed.

start: Numeric variable that defines the initial value at the beginning of the loop.

end: Numeric variable that defines the final value at the end of the loop.

Step: seab sammuvĂ€Ă€rtuse, mille vĂ”rra tsĂŒklite arvu suurendada vĂ”i vĂ€hendada. Kui step on mĂ€Ă€ramata, siis suurendatakse tsĂŒklite arvu 1 vĂ”rra. Sel juhul peab end olema suurem kui start. Kui soovid vĂ€Ă€rtust counter vĂ€hendada, siis peab vĂ€Ă€rtus end olema vĂ€iksem kui vĂ€Ă€rtus start ja step peab olema negatiivne vĂ€Ă€rtus.

TsĂŒkkel For...Next kordab kĂ”iki tsĂŒklis olevaid lauseid parameetritega mĂ€Ă€ratud arv kordi.

As the counter variable is decreased, LibreOffice Basic checks if the end value has been reached. As soon as the counter passes the end value, the loop automatically terminates.

TsĂŒkli For...Next lauseid saab pesastada. Kui lausele Next jĂ€rgnevat muutujat pole mĂ€Ă€ratud, viitab Next automaatselt viimatisele For-lausele.

Kui mĂ€Ă€rad juurdekasvuks 0, siis korratakse lausete For ja Next vahelisi lauseid pidevalt.

When counting down the counter variable, LibreOffice Basic checks for overflow or underflow. The loop ends when counter exceeds end (positive Step value) or is less than end (negative Step value).

TsĂŒklist tingimusteta vĂ€ljumiseks kasuta lauset VĂ€lju lausest For. See lause peab olema tsĂŒklis For...Next. VĂ€ljumistingimuse testimiseks kasuta lauset If...Then jĂ€rgmiselt:

``````
For...
lause sisu
If condition = True Then Exit For
lause sisu
Next
``````

In nested For...Next loops, if you exit a loop unconditionally with Exit For, only one loop is exited.

NĂ€ited

The following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that is filled with various contents:

``````
Sub ExampleSort
Dim sEntry(9) As String
Dim iCount As Integer, iCount2 As Integer
Dim sTemp As String
sEntry = Array("Jerry","Patty","Kurt","Thomas","Michael",_
"David","Cathy","Susie","Edward","Christine")
For iCount = 0 To 9
For iCount2 = iCount + 1 To 9
If sEntry(iCount) > sEntry(iCount2) Then
sTemp = sEntry(iCount)
sEntry(iCount) = sEntry(iCount2)
sEntry(iCount2) = sTemp
End If
Next iCount2
Next iCount
For iCount = 0 To 9
Print sEntry(iCount)
Next iCount
End Sub
``````

This explores the content of an array to display each item it contains.

``````
Sub list_iteration
cutlery = Array("fork", "knife", "spoon")
For Each item in cutlery
Print item
Next ' item
End Sub
``````

Palun toeta meid!