<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Floops</id>
	<title>EN/documentation/basic guide/loops - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.harlamenkov.ru/index.php?action=history&amp;feed=atom&amp;title=EN%2Fdocumentation%2Fbasic_guide%2Floops"/>
	<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/loops&amp;action=history"/>
	<updated>2026-11-11T13:51:13Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.0</generator>
	<entry>
		<id>https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/loops&amp;diff=15888&amp;oldid=prev</id>
		<title>Sancho: Created page with &quot;&lt;section begin=title /&gt;&lt;noinclude&gt;{{DISPLAYTITLE:&lt;/noinclude&gt;Loops&lt;noinclude&gt;}}&lt;/noinclude&gt;&lt;section end=title /&gt;{{BreadCrumbL10n}}  {{Documentation/BASICGuideTOC |ShowPrevNext=bl...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.harlamenkov.ru/index.php?title=EN/documentation/basic_guide/loops&amp;diff=15888&amp;oldid=prev"/>
		<updated>2011-09-01T14:32:29Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Loops&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}  {{Documentation/BASICGuideTOC |ShowPrevNext=bl...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;section begin=title /&amp;gt;&amp;lt;noinclude&amp;gt;{{DISPLAYTITLE:&amp;lt;/noinclude&amp;gt;Loops&amp;lt;noinclude&amp;gt;}}&amp;lt;/noinclude&amp;gt;&amp;lt;section end=title /&amp;gt;{{BreadCrumbL10n}}&lt;br /&gt;
&lt;br /&gt;
{{Documentation/BASICGuideTOC&lt;br /&gt;
|ShowPrevNext=block&lt;br /&gt;
|ShowPrevPage=block&lt;br /&gt;
|PrevPage=EN/documentation/basic_guide/branching&lt;br /&gt;
|NextPage=EN/documentation/basic_guide/procedures_and_functions&lt;br /&gt;
|lang=block&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
__NOTOC__&lt;br /&gt;
&lt;br /&gt;
A loop executes a code block for the number of passes that are specified. You can also have loops with an undefined number of passes. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;tt&amp;gt;For...Next&amp;lt;/tt&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;For...Next&amp;lt;/tt&amp;gt; loop has a fixed number of passes. The loop counter defines the number of times that the loop is to be executed. In the following example, variable &amp;lt;tt&amp;gt;I&amp;lt;/tt&amp;gt; is the loop counter, with an initial value of 1. The counter is incremented by 1 at the end of each pass. When variable I equals 10, the loop stops.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim I &lt;br /&gt;
For I = 1 To 10&lt;br /&gt;
  ' ...  Inner part of loop &lt;br /&gt;
Next I&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to increment the loop counter by a value other than 1 at the end of each pass, use the &amp;lt;tt&amp;gt;Step&amp;lt;/tt&amp;gt; function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim I &lt;br /&gt;
For I = 1 To 10 Step 0.5&lt;br /&gt;
  ' ... Inner part of loop &lt;br /&gt;
Next I&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the preceding example, the counter is increased by 0.5 at the end of each pass and the loop is executed 19 times.&lt;br /&gt;
&lt;br /&gt;
You can also use negative step values:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim I &lt;br /&gt;
For I = 10 To 1 Step -1&lt;br /&gt;
  ' ... Inner part of loop &lt;br /&gt;
Next I&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, the counter begins at 10 and is reduced by 1 at the end of each pass until the counter is 1.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Exit For&amp;lt;/tt&amp;gt; instruction allows you to exit a &amp;lt;tt&amp;gt;For&amp;lt;/tt&amp;gt; loop prematurely. In the following example, the loop is terminated during the fifth pass:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Dim I &lt;br /&gt;
For I = 1 To 10 &lt;br /&gt;
  If I = 5 Then &lt;br /&gt;
    Exit For&lt;br /&gt;
  End If&lt;br /&gt;
  ' ... Inner part of loop &lt;br /&gt;
Next I&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;tt&amp;gt;For Each&amp;lt;/tt&amp;gt; ==&lt;br /&gt;
The &amp;lt;tt&amp;gt;For Each...Next&amp;lt;/tt&amp;gt; loop variation in VBA is supported in {{OOo}} Basic.  &amp;lt;tt&amp;gt;For Each&amp;lt;/tt&amp;gt; loops do not use an explicit counter like a &amp;lt;tt&amp;gt;For...Next&amp;lt;/tt&amp;gt; loop does. A &amp;lt;tt&amp;gt;For Each&amp;lt;/tt&amp;gt; loop says &amp;quot;do this to everything in this set&amp;quot;, rather than &amp;quot;do this n times&amp;quot;.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Const d1 = 2&lt;br /&gt;
Const d2 = 3&lt;br /&gt;
Const d3 = 2&lt;br /&gt;
Dim i&lt;br /&gt;
Dim a(d1, d2, d3)&lt;br /&gt;
For Each i In a()&lt;br /&gt;
    ' ... Inner part of loop &lt;br /&gt;
Next i&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The loop will execute 36 times.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;tt&amp;gt;Do...Loop&amp;lt;/tt&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Do...Loop&amp;lt;/tt&amp;gt; is not linked to a fixed number of passes. Instead, the &amp;lt;tt&amp;gt;Do...Loop&amp;lt;/tt&amp;gt; is executed until a certain condition is met. There are four versions of the &amp;lt;tt&amp;gt;Do...Loop&amp;lt;/tt&amp;gt;. In the first two examples, the code within the loop may not be executed at all (&amp;quot;do 0 times&amp;quot; logic).  In the latter examples, the code will be executed at least once. (In the following examples, &amp;lt;tt&amp;gt;A &amp;gt; 10&amp;lt;/tt&amp;gt; represents any condition):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol class=&amp;quot;task&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;The &amp;lt;tt&amp;gt;Do While...Loop&amp;lt;/tt&amp;gt; version&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do While A &amp;gt; 10&lt;br /&gt;
   ' ... loop body&lt;br /&gt;
Loop&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
checks whether the condition after the &amp;lt;tt&amp;gt;While&amp;lt;/tt&amp;gt; is '''true''' before every pass and only then executes the loop.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;The &amp;lt;tt&amp;gt;Do Until...Loop&amp;lt;/tt&amp;gt; version&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do Until A &amp;gt; 10&lt;br /&gt;
  ' ... loop body&lt;br /&gt;
Loop&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
executes the loop as long as the condition after the &amp;lt;tt&amp;gt;Until&amp;lt;/tt&amp;gt; evaluates to '''false'''.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;The &amp;lt;tt&amp;gt;Do...Loop While&amp;lt;/tt&amp;gt; version&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do&lt;br /&gt;
  ' ... loop body&lt;br /&gt;
Loop While A &amp;gt; 10&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
only checks the condition after the first loop pass and terminates if the condition after the &amp;lt;tt&amp;gt;While&amp;lt;/tt&amp;gt; evaluates to '''false'''.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;The &amp;lt;tt&amp;gt;Do...Loop Until&amp;lt;/tt&amp;gt; version&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do&lt;br /&gt;
  ' ... loop body&lt;br /&gt;
Loop Until A &amp;gt; 10&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
also checks its condition after the first pass, but terminates if the condition after the &amp;lt;tt&amp;gt;Until&amp;lt;/tt&amp;gt; evaluates to '''true'''.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As in the &amp;lt;tt&amp;gt;For...Next&amp;lt;/tt&amp;gt; loop, the &amp;lt;tt&amp;gt;Do...Loop&amp;lt;/tt&amp;gt; also provides a terminate command. The &amp;lt;tt&amp;gt;Exit Do&amp;lt;/tt&amp;gt; command can exit at loop at any point within the loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do &lt;br /&gt;
  If A = 4 Then&lt;br /&gt;
    Exit Do&lt;br /&gt;
  End If&lt;br /&gt;
  ' ... loop body&lt;br /&gt;
Loop While A &amp;gt; 10&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In some cases the loop may only terminate when a condition is met within the loop. Then you can use the &amp;quot;perpetual&amp;quot; Do Loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do &lt;br /&gt;
  ' ... some internal calculations&lt;br /&gt;
  If A = 4 Then    Exit Do&lt;br /&gt;
  ' ... other instructions&lt;br /&gt;
Loop&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;tt&amp;gt;While...Wend&amp;lt;/tt&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;While...Wend&amp;lt;/tt&amp;gt; loop construct works exactly the same as the &amp;lt;tt&amp;gt;Do While...Loop&amp;lt;/tt&amp;gt;, but with the disadvantage that there is no &amp;lt;tt&amp;gt;Exit&amp;lt;/tt&amp;gt; command available. The following two loops produce identical results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Do While A &amp;gt; 10&lt;br /&gt;
   ' ... loop body&lt;br /&gt;
Loop&lt;br /&gt;
&lt;br /&gt;
While A &amp;gt; 10&lt;br /&gt;
   ' ... loop body&lt;br /&gt;
Wend&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Programming Example: Sorting With Embedded Loops ==&lt;br /&gt;
&lt;br /&gt;
There are many ways to use loops, for example, to search lists, return values, or execute complex mathematical tasks. The following example is an algorithm that uses two loops to sort a list by names.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;oobas&amp;quot;&amp;gt;&lt;br /&gt;
Sub Sort&lt;br /&gt;
  Dim Entry(1 To 10) As String&lt;br /&gt;
  Dim Count As Integer&lt;br /&gt;
  Dim Count2 As Integer&lt;br /&gt;
  Dim Temp As String&lt;br /&gt;
&lt;br /&gt;
  Entry(1) = &amp;quot;Patty&amp;quot;&lt;br /&gt;
  Entry(2) = &amp;quot;Kurt&amp;quot;&lt;br /&gt;
  Entry(3) = &amp;quot;Thomas&amp;quot;&lt;br /&gt;
  Entry(4) = &amp;quot;Michael&amp;quot;&lt;br /&gt;
  Entry(5) = &amp;quot;David&amp;quot;&lt;br /&gt;
  Entry(6) = &amp;quot;Cathy&amp;quot;&lt;br /&gt;
  Entry(7) = &amp;quot;Susie&amp;quot;&lt;br /&gt;
  Entry(8) = &amp;quot;Edward&amp;quot;&lt;br /&gt;
  Entry(9) = &amp;quot;Christine&amp;quot;&lt;br /&gt;
  Entry(10) = &amp;quot;Jerry&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  For Count = 1 To 9&lt;br /&gt;
    For Count2 = Count + 1 To 10&lt;br /&gt;
      If Entry(Count) &amp;gt; Entry(Count2) Then&lt;br /&gt;
        Temp = Entry(Count)&lt;br /&gt;
        Entry(Count) = Entry(Count2)&lt;br /&gt;
        Entry(Count2) = Temp&lt;br /&gt;
      End If&lt;br /&gt;
    Next Count2&lt;br /&gt;
  Next Count&lt;br /&gt;
&lt;br /&gt;
  For Count = 1 To 10&lt;br /&gt;
    Print Entry(Count)&lt;br /&gt;
  Next Count&lt;br /&gt;
&lt;br /&gt;
End Sub&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The values are interchanged as pairs several times until they are finally sorted in ascending order. Like bubbles, the variables gradually migrate to the right position. For this reason, this algorithm is also known as a [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort].&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
{{PDL1}}&lt;/div&gt;</summary>
		<author><name>Sancho</name></author>
		
	</entry>
</feed>