summaryrefslogtreecommitdiffstats
path: root/helpcontent2/source/text/sbasic/shared/03090202.xhp
blob: 51a51f1dbf334957fcb96ddfa267fb85120f48e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->

<meta>
  <topic id="textsbasicshared03090202xml" indexer="include" status="PUBLISH">
    <title id="tit" xml-lang="en-US">For...Next Statement</title>
    <filename>/text/sbasic/shared/03090202.xhp</filename>
  </topic>
</meta>

<body>


<section id="fornext">
<bookmark xml-lang="en-US" branch="index" id="bm_id3149205">
<bookmark_value>For statement</bookmark_value>
<bookmark_value>For Each statement</bookmark_value>
<bookmark_value>In keyword</bookmark_value>
<bookmark_value>Next keyword</bookmark_value>
<bookmark_value>Step keyword</bookmark_value>
<bookmark_value>To keyword</bookmark_value>
</bookmark>


<h1 id="hd_id3149205" xml-lang="en-US"><link href="text/sbasic/shared/03090202.xhp">For...Next Statement</link></h1>
  <paragraph id="par_id3143267" role="paragraph" xml-lang="en-US">Repeats the statements between the <literal>For...Next</literal> block a specified number of times.</paragraph>
</section>

<h2 id="hd_id3156153" xml-lang="en-US">Syntax:</h2>
<paragraph role="paragraph" id="par_id491585753339474">
  <image src="media/helpimg/sbasic/For-Next_statement.svg" id="img_id4156296484514"><alt xml-lang="en-US" id="alt_id15152796484514">For Statement diagram</alt></image>
</paragraph>
<bascode>
<paragraph id="par_id3148473" role="bascode" localize="false">For counter=start To end [Step step]</paragraph>
<paragraph id="par_id3156024" role="bascode" xml-lang="en-US">    statement-block</paragraph>
<paragraph id="par_id3146796" role="bascode" localize="false">    [Exit For]</paragraph>
<paragraph id="par_id3159414" role="bascode" xml-lang="en-US">    statement-block</paragraph>
<paragraph id="par_id3153897" role="bascode" localize="false">Next [counter]</paragraph>
</bascode>
<paragraph role="paragraph" id="par_id491585653339474">
  <image src="media/helpimg/sbasic/For-Each_statement.svg" id="img_id4156297484514"><alt xml-lang="en-US" id="alt_id15152797484514">For Each Statement diagram</alt></image>
</paragraph>
<bascode>
  <paragraph role="bascode" id="bas_id331586521232310" localize="false">For Each item In list</paragraph>
  <paragraph role="bascode" id="bas_id821586521234861" xml-lang="en-US">    statement-block</paragraph>
  <paragraph role="bascode" id="bas_id991586521235339" localize="false">[Exit For]</paragraph>
  <paragraph role="bascode" id="bas_id501586521235517" xml-lang="en-US">    statement-block</paragraph>
  <paragraph role="bascode" id="bas_id941586521235718" localize="false">Next [item]</paragraph>
</bascode>

<h2 id="hd_id3150400" xml-lang="en-US">Variables:</h2>
<paragraph id="par_id3150358" role="paragraph" xml-lang="en-US"> <emph>counter:</emph> Loop <literal>counter</literal> initially assigned the value to the right of the equal sign (<literal>start</literal>). Only numeric variables are valid. The loop counter increases or decreases according to the variable <literal>step</literal> until <literal>end</literal> is passed.</paragraph>
<paragraph id="par_id3152455" role="paragraph" xml-lang="en-US"> <emph>start:</emph> Numeric variable that defines the initial value at the beginning of the loop.</paragraph>
<paragraph id="par_id3151043" role="paragraph" xml-lang="en-US"> <emph>end:</emph> Numeric variable that defines the final value at the end of the loop.</paragraph>
<paragraph id="par_id3156281" role="paragraph" xml-lang="en-US"> <emph>step:</emph> Sets the value by which to increase or decrease the loop counter. If <literal>step</literal> is not specified, the loop counter is incremented by 1. In this case, <literal>end</literal> must be greater than <literal>start</literal>. If you want to decrease <literal>counter</literal>, <literal>end</literal> must be less than <literal>start</literal>, and <literal>step</literal> must be assigned a negative value.</paragraph>
<paragraph id="par_id3154684" role="paragraph" xml-lang="en-US">The <emph>For...Next</emph> loop repeats all of the statements in the loop for the number of times that is specified by the parameters.</paragraph>
<paragraph id="par_id3147287" role="paragraph" xml-lang="en-US">As the <literal>counter</literal> variable is decreased, %PRODUCTNAME Basic checks if the <literal>end</literal> value has been reached. As soon as the <literal>counter</literal> passes the <literal>end</literal> value, the loop automatically terminates.</paragraph>
<paragraph id="par_id3159154" role="paragraph" xml-lang="en-US">It is possible to nest <emph>For...Next</emph> statements. If you do not specify a variable following the <emph>Next</emph> statement, <emph>Next</emph> automatically refers to the most recent <emph>For</emph> statement.</paragraph>
<paragraph id="par_id3155306" role="paragraph" xml-lang="en-US">If you specify an increment of 0, the statements between <emph>For</emph> and <emph>Next</emph> are repeated continuously.</paragraph>
<paragraph id="par_id3155854" role="paragraph" xml-lang="en-US">When counting down the counter variable, %PRODUCTNAME Basic checks for overflow or underflow. The loop ends when <literal>counter</literal> exceeds <literal>end</literal> (positive Step value) or is less than <literal>end</literal> (negative Step value).</paragraph>
<paragraph id="par_id3145273" role="paragraph" xml-lang="en-US">Use the <emph>Exit For</emph> statement to exit the loop unconditionally. This statement must be within a <emph>For...Next</emph> loop. Use the <emph>If...Then</emph> statement to test the exit condition as follows:</paragraph>
<bascode>
  <paragraph role="bascode" id="bas_id631586522581351" localize="false">For...</paragraph>
  <paragraph role="bascode" id="bas_id621586522583437" xml-lang="en-US">    statement-block</paragraph>
  <paragraph role="bascode" id="bas_id571586522583814" localize="false">    If condition = True Then Exit For</paragraph>
  <paragraph role="bascode" id="bas_id711586522584013" xml-lang="en-US">    statement-block</paragraph>
  <paragraph role="bascode" id="bas_id831586522584527" localize="false">Next</paragraph>
</bascode>

<note id="par_id3156286" xml-lang="en-US">In nested <emph>For...Next</emph> loops, if you exit a loop unconditionally with <emph>Exit For</emph>, only one loop is exited.</note>

<h2 id="hd_id3148457" xml-lang="en-US">Examples</h2>
<paragraph id="par_id3151074" role="paragraph" xml-lang="en-US">The following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that is filled with various contents:</paragraph>
<bascode>
<paragraph id="par_idm1341539088" role="bascode" localize="false">Sub ExampleSort</paragraph>
<paragraph id="par_idm1340517312" role="bascode" localize="false">Dim sEntry(9) As String</paragraph>
<paragraph id="par_idm1340516080" role="bascode" localize="false">Dim iCount As Integer, iCount2 As Integer</paragraph>
<paragraph id="par_idm1340513616" role="bascode" localize="false">Dim sTemp As String</paragraph>
<paragraph id="par_id31557349267" role="bascode" localize="false">    sEntry = Array("Jerry","Patty","Kurt","Thomas","Michael",_</paragraph>
<paragraph id="par_id49231539711" role="bascode" localize="false">                 "David","Cathy","Susie","Edward","Christine")</paragraph>
<paragraph id="par_idm1340495152" role="bascode" localize="false">    For iCount = 0 To 9</paragraph>
<paragraph id="par_idm1340493920" role="bascode" localize="false">        For iCount2 = iCount + 1 To 9</paragraph>
<paragraph id="par_idm1340492672" role="bascode" localize="false">            If sEntry(iCount) &gt; sEntry(iCount2) Then</paragraph>
<paragraph id="par_idm1340491296" role="bascode" localize="false">                sTemp = sEntry(iCount)</paragraph>
<paragraph id="par_idm1340490096" role="bascode" localize="false">                sEntry(iCount) = sEntry(iCount2)</paragraph>
<paragraph id="par_idm1340488832" role="bascode" localize="false">                sEntry(iCount2) = sTemp</paragraph>
<paragraph id="par_idm1340487584" role="bascode" localize="false">            End If</paragraph>
<paragraph id="par_idm1340486352" role="bascode" localize="false">        Next iCount2</paragraph>
<paragraph id="par_idm1340485120" role="bascode" localize="false">    Next iCount</paragraph>
<paragraph id="par_idm1340483888" role="bascode" localize="false">    For iCount = 0 To 9</paragraph>
<paragraph id="par_idm1340482656" role="bascode" localize="false">        Print sEntry(iCount)</paragraph>
<paragraph id="par_idm1340481408" role="bascode" localize="false">    Next iCount</paragraph>
<paragraph id="par_idm1340480176" role="bascode" localize="false">End Sub</paragraph>
</bascode>
<paragraph role="paragraph" id="par_id561586524231943">This explores the content of an array to display each item it contains.</paragraph>
<bascode>
  <paragraph role="bascode" id="bas_id511586523090639">Sub list_iteration</paragraph>
  <paragraph role="bascode" id="bas_id391586523091799">    cutlery = Array("fork", "knife", "spoon")</paragraph>
  <paragraph role="bascode" id="bas_id811586523092655">    For Each item in cutlery</paragraph>
  <paragraph role="bascode" id="bas_id271586523092911">        Print item</paragraph>
  <paragraph role="bascode" id="bas_id941586523093415">    Next ' item</paragraph>
  <paragraph role="bascode" id="bas_id301586523093607">End Sub</paragraph>
</bascode>
</body>

</helpdocument>