Expressió For… Next

Repeats the statements between the For...Next block a specified number of times.

Sintaxi:

per al diagrama d'extracte


For counter=start To end [Step step]
    bloc d'expressions
    [Exit For]
    bloc d'expressions
Next [counter]

Per a cada diagrama d'extracte


  For Each item In list
      bloc d'expressions
  [Exit For]
      bloc d'expressions
  Next [item]

Variables:

contra Loop contra inicialment assignava el valor a la dreta del signe igual (inicia). Només són vàlides les variables numèriques. El comptador de bucles augmenta o disminueix segons la variable pas fins que es passa final.

inicia la variable Numeric que defineix el valor inicial al començament del bucle.

end Variable numèrica que defineix el valor final al final del bucle.

step: Sets the value by which to increase or decrease the loop counter. If step is not specified, the loop counter is incremented by 1. In this case, end must be greater than start. If you want to decrease counter, end must be less than start, and step must be assigned a negative value.

El bucle For...Next repeteix totes les expressions del bucle per al nombre de vegades que s'especifica als paràmetres.

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.

Es poden imbricar expressions For...Next. Si no especifiqueu una variable que segueixi l'expressió Next, Next fa referència automàticament a l'expressió For més recent.

Si especifiqueu un increment de 0, les expressions entre For i Next es repeteixen contínuament.

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).

Utilitzeu l'expressió Exit For per a sortir incondicionalment del bucle. Aquesta expressió ha d'estar a dins d'un bucle For...Next. Utilitzeu l'expressió If...Then per a provar la condició de sortida de la forma següent:


  For...
      bloc d'expressions
      If condition = True Then Exit For
      bloc d'expressions
  Next
note

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


Exemples

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

Ens cal la vostra ajuda!