Repository navigation
For Next
Control flow statement for looping
For iterator [ As datatype ] = startvalue To endvalue [ Step stepvalue ]
[ statement block ]
Next [ iterator ]iteratordatatypeiteratordatatypestartvalueendvaluestepvalue
A For...Next loop initializes iterator to startvalue, then executes the statement block, incrementing iterator by stepvalue until it exceeds endvalue. If stepvalue is not explicitly given it will set to 1.
The values of stepvalue and endvalue are stored internally immediately following execution of the For statement and thus neither can be changed inside the For loop. Comparison operators such as < and > will not be effective as stepvalue or endvalue because the expressions will not be re-evaluated while the loop is running. (The results of the expressions used to define them may be changed, but these changes will not affect the execution of the For loop.) See examples.
Note: In some dialects, the temporary variables holding stepvalue and endvalue go out of scope at the end of the loop, and their values are not guaranteed to remain unchanged once any code following the loop has been executed. For this reason, it is recommended never to branch out of a For...Next loop (using Goto or similar), and then jump back into the middle of it later when in the -lang fb or -lang deprecated dialect.
The iterator must be an intrinsic scalar: only Static/Shared variables and local variables can be used; no other kind can be used, including array elements, UDT members, ByRef parameters or any kind of dereferenced address.
The iterator may be defined having the same scope as the For statement by using the As datatype syntax. With this syntax, iterator is created and destroyed within the For...Next scope. See dialect differences below.
If endvalue is less than startvalue then a negative stepvalue must be specified or the statement block will not execute at all, since startvalue compares greater than endvalue.
The For statement causes the execution of the statements in the statement block until iterator compares greater than endvalue (or less than endvalue if stepvalue < 0). iterator will be incremented the amount of stepvalue following each execution of the statement block. If an increment is not given, iterator will be implicitly incremented by 1.
If an Exit For statement is encountered inside the statement block, the loop is terminated, and execution resumes immediately following the enclosing Next statement. If a Continue For statement is encountered, the rest of the statement block is skipped until the block's corresponding Next statement. The counter's value is incremented and the loop restarted if it is still within the bounds given by endvalue.
Note: for integer data types, it is not possible to loop up to the highest possible value (or down to the lowest possible value) that can be stored in the variable type, because the loop only breaks when the incremented variable exceeds endvalue, which can never happen. For example, if you try to iterate an variable from 0 to 255, the loop will only break once the variable reaches 256 or more. Using a UByte variable for the counter wouldn't work, because although it can hold the numbers 0 to 255, it cannot hold 256. See Standard Data Type Limits to find the upper and lower limits for the standard data types.
Like all control flow statements, the For statement can be nested, that is, it can be used in a statement block of another For statement.
For, Next, and Step are operators that can be overloaded inside user defined types. See Operator For, Operator Next, Operator Step
- In the -lang fb and -lang deprecated dialects, variables declared inside a
For..Nextblock are visible only inside the block, and cannot be accessed outside it. To access duplicated symbols defined as global outside this block, add one or preferably two dot(s) as prefix:.SomeSymbolor preferably..SomeSymbol(or only..SomeSymbolif inside a With..End With block). In the -lang qb and -lang fblite dialects, variables declared inside aFor..Nextblock (including the counter if declared, and any temporary variables used to holdendvalueorstepvalue), have a procedure-wide scope as in QB
-
ByRefarguments cannot be used as counters.
Print "counting from 3 to 0, with a step of -0.5"
For i As Single = 3 To 0 Step -0.5
Print "i is " & i
Next iDim As Integer i, j, k, toTemp, stepTemp
j = 9: k = 1
For i = 0 To j Step k
j = 0: k = 0 '' Changing j and k has no effect on the current loop.
Print i;
Next i
Print
' Internally, this is what the above example does:
j = 9: k = 1
i = 0: toTemp = j: stepTemp = k
Do While IIf(stepTemp >= 0, i <= toTemp, i >= toTemp)
j = 0: k = 0 '' Changing j and k has no effect on the current loop.
Print i;
i += stepTemp
Loop
PrintExample of infinite For...Next loop in case of loop up to the highest value of an Ubyte iterator (255):
For ub As UByte = 240 To 255 '' Infinite loop because the end criterion value (255+1=256) can never be reached by the UByte iterator
Print ub
If Inkey <> "" Then Exit For
Sleep 10
Next ubThe same applies to lowest value of an UByte iterator (0) in case of loop down (negative stepvalue).
Category: Operators
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing