Module
Contents
Index 🔎︎

# For...Next Statement

Repite as instrucións entre o bloque For... Next un número especificado de veces.

## Sintaxe:

    For contador=inicio To fin [Step incremento]

    bloque de instrucións

    [Exit For]

    bloque de instrucións

    Next [contador]

## Variábeis:

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

The For...Next loop repeats all of the statements in the loop for the number of times that is specified by the parameters.

A medida que a variábel do contador diminúe, LibreOffice Basic verifica se se atinxiu o valor final. Así que o contador chega ao valor final o lazo finaliza automaticamente.

It is possible to nest For...Next statements. If you do not specify a variable following the Next statement, Next automatically refers to the most recent For statement.

If you specify an increment of 0, the statements between For and Next are repeated continuously.

Ao facer a conta atrás coa variábel contador, LibreOffice Basic verifica que non se produce rebordamento ou vacuidade. O lazo finaliza cando o contador supera a fin (paso positivo) ou é menor que a fin (paso negativo).

Use the Exit For statement to exit the loop unconditionally. This statement must be within a For...Next loop. Use the If...Then statement to test the exit condition as follows:

For...

instrucións

If condición = verdadeira Then Exit For

instrucións

Seguinte

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

## Exemplo

O seguinte exemplo usa dous lazos aniñados para ordenar unha matriz de cadea con 10 elementos ( sEntry() ), que primeiro se enchen con varios contidos:

    Sub ExampleSort

    Dim sEntry(9) As String

    Dim iCount As Integer

    Dim iCount2 As Integer

    Dim sTemp As String

        sEntry(0) = "Xabier"

        sEntry(1) = "Paula"

        sEntry(2) = "Antón"

        sEntry(3) = "Xosé"

        sEntry(4) = "Manuel"

        sEntry(5) = "Martiño"

        sEntry(6) = "Xiana"

        sEntry(7) = "Susana"

        sEntry(8) = "Duarte"

        sEntry(9) = "Xosefa"

        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