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]
    [Exit For]
Next [counter]

For Each Statement diagram

  For Each item In list
  [Exit For]
  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 αžŠαŸ‚αž›β€‹αžαŸ’αž˜αžΈβ€‹αž”αŸ†αž•αž»αžΒ αŸ”

αž”αŸ’αžšαžŸαž·αž“β€‹αž”αžΎβ€‹αž’αŸ’αž“αž€β€‹αž”αž‰αŸ’αž‡αžΆαž€αŸ‹β€‹β€‹β€‹αž…αŸ†αž“αž½αž“β€‹αž”αž“αŸ’αžαŸ‚αž˜ ០ αž“αŸ„αŸ‡β€‹αžŸαŸαž…αž€αŸ’αžαžΈβ€‹αžαŸ’αž›αŸ‚αž„β€‹αž€αžΆαžšαžŽαŸβ€‹αž“αŸ…β€‹αž…αž“αŸ’αž›αŸ„αŸ‡ 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 αžŠαžΎαž˜αŸ’αž”αžΈαžŸαžΆαž€αž›αŸ’αž”αž„β€‹β€‹αž›αž€αŸ’αžαžαžŽαŸ’αžŒβ€‹αž…αŸαž‰β€‹β€‹αžŠαžΌαž…β€‹αžαžΆαž„β€‹αž€αŸ’αžšαŸ„αž˜ αŸ–

      If condition = True Then Exit For

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",_
    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!