For...Next Statement

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

āϏāĻŋāύāĻŸā§āϝāĻžāĻ•ā§āϏ:

For Statement diagram


For counter=start To end [Step step]
    statement-block
    [Exit For]
    statement-block
Next [counter]

For Each Statement diagram


  For Each item In list
      statement-block
  [Exit For]
      statement-block
  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: 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.

āĻĒā§āϝāĻžāϰāĻžāĻŽāĻŋāϟāĻžāϰ⧇ āωāĻ˛ā§āϞ⧇āĻ–āĻŋāϤ āϏāĻŽā§Ÿā§‡āϰ āϜāĻ¨ā§āϝ āϞ⧁āĻĒ⧇ For...Next āϞ⧁āĻĒ⧇āϰ āϏāĻžāĻšāĻžāĻ¯ā§āϝ⧇ āϏāĻ•āϞ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āĻĒ⧁āύāϰāĻžāĻŦ⧃āĻ¤ā§āϤāĻŋ āĻ•āϰ⧇ āĻĨāĻžāϕ⧇āĨ¤

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.

For...Next āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āĻāĻ•āĻ¤ā§āϰāĻŋāϤ āĻ•āϰāĻž āϏāĻŽā§āĻ­āĻŦāĨ¤ Next āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āĻ…āύ⧁āϏāϰāĻŖāĻ•āĻžāϰ⧀ āĻāĻ•āϟāĻŋ āϭ⧇āϰāĻŋā§Ÿā§‡āĻŦāϞ āωāĻ˛ā§āϞ⧇āĻ– āύāĻž āĻ•āϰ⧇āύ, āϤāĻžāĻšāϞ⧇ Next āĻ¸ā§āĻŦ⧟āĻ‚āĻ•ā§āϰāĻŋ⧟āĻ­āĻžāĻŦ⧇ āϏāĻŦāĻšā§‡ā§Ÿā§‡ āϏāĻžāĻŽā§āĻĒā§āϰāϤāĻŋāĻ• For āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟāϟāĻŋ āύāĻŋāĻ°ā§āĻĻ⧇āĻļāύ āĻ•āϰ⧇ āĻĨāĻžāϕ⧇āĨ¤

āϝāĻĻāĻŋ 0 āĻāϰ āĻŦ⧃āĻĻā§āϧāĻŋ āωāĻ˛ā§āϞ⧇āĻ– āĻ•āϰāĻž āĻšā§Ÿ, āϤāĻžāĻšāϞ⧇ For āĻāĻŦāĻ‚ Next āĻāϰ āĻŽāĻ§ā§āϝ⧇ āĻŦāĻŋāĻĻā§āϝāĻŽāĻžāύ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āϧāĻžāϰāĻžāĻŦāĻžāĻšāĻŋāĻ•āĻ­āĻžāĻŦ⧇ āĻĒ⧁āύāϰāĻžāĻŦ⧃āĻ¤ā§āϤāĻŋ āĻ•āϰāĻž āĻšā§ŸāĨ¤

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

āĻāĻ•āϟāĻŋ āϞ⧁āĻĒ āĻĨ⧇āϕ⧇ āĻļāĻ°ā§āϤāĻšā§€āύāĻ­āĻžāĻŦ⧇ āĻĒā§āϰāĻ¸ā§āĻĨāĻžāύ āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ Exit For āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟāϟāĻŋ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧁āύāĨ¤ āĻāχ āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟāϟāĻŋ āĻ…āĻŦāĻļā§āϝāχ For...Next āϞ⧁āĻĒ⧇āϰ āĻŽāĻ§ā§āϝ⧇ āĻĨāĻžāĻ•āϤ⧇ āĻšāĻŦ⧇āĨ¤ āύāĻŋāĻŽā§āύāĻžāύ⧁āϏāĻžāϰ⧇ āĻĒā§āϰāĻ¸ā§āĻĨāĻžāύ āĻļāĻ°ā§āϤāϟāĻŋ āĻĒāϰ⧂āĻ•ā§āώāϪ⧇āϰ āϜāĻ¨ā§āϝ If...Then āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟāϟāĻŋ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧁āύ:


  For...
      statement-block
      If condition = True Then Exit For
      statement-block
  Next
note

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


āωāĻĻāĻžāĻšāϰāĻŖāϏāĻŽā§‚āĻš

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

Please support us!