![]() |
<< Index >> |
FutureBasic 5 |
'for' standard loop & 'for in' fast enumeration loop | statement | |
|
for indexVar = firstValue to lastValue [STEP stepValue]
[statementBlock]
next [indexVar]
for
statement marks the beginning of a "for-loop," which must end with a next
statement. A for-loop is useful when you want to repeat the execution of a block of statements for a particular number of times. This is what happens when a for-loop is encountered:firstValue
is assigned to indexVar
(indexVar
must be a simple numeric variable).
statementBlock
are executed. statementBlock
can contain any number of statements, possibly including other for-loops (but note that any for-loop that's "nested" within statementBlock
should not use the same indexVar
as the "outer" for-loop).
indexVar
+ stepValue
is assigned to indexVar
. (If you omit the STEP
stepValue
clause, then incremental value defaults to 1.)
indexVar
is compared with lastValue
, to see whether the loop should be repeated:stepValue
is positive, then repeat the loop (go to Step 2) if indexVar
<=lastValue
.stepValue
is negative, then repeat the loop (go to Step 2) if indexVar
>=lastValue
.
for n = 3 to sqr(x!) STEP 2
:
next
sqr
function is called after each iteration of the loop. Assuming that the value of x!
doesn't change within the loop, we are needlessly recalculating the same sqr
value at each iteration. It would be much faster to do it this way:sqrx! = sqr(x!)
for n = 3 to sqrx! STEP 2
:
next
sqr
function is called only once.dim as long j
for j = 1 to 0
print "Never get here"
next
override _forLoopsAlwaysExecuteAtLeastOnce = _true
for j = 1 to 0
print "Get here" // legacy FutureBasic behavior
next
override _forLoopsAlwaysExecuteAtLeastOnce = _false
for j = 1 to 0
print "Never get here"
next
statementBlock
has been met. The standard way to do this is to use exit for
.for p = 1 to maxStrings
long if strArray(p) = searchPascalString
found = _zTrue
theIndex = p
exit for 'force early exit from loop
end if
next
'for in'
loop ( introduced in FB 5.7.116 )
Syntax:
for cfObjectVar in cfCollectionVar [STEP-1]
[statementBlock]
next
Description:
'for in'
provides simplified access to Core Foundation collection objects such as arrays and dictionaries.'for in'
marks the beginning of a fast enumeration loop. It is equivalent to Objective-C's fast enumeration and operates similarly.'for in'
enumerates and supports the following Core Foundation collection objects: CFArrayRef, CFMutableArrayRef, CFDictionaryRef, CFMutableDictionaryRef, CFSetRef, CFMutableSetRef, OrderedSetRef, MutableOrderedSetRef, CountedSetRef, and PointerArrayRef.
'for in'
must end with a next
statement. A 'for in'
loop is useful for iterating and processing individual items within a collection.statementBlock
statements are executed and the contents of cfCollectionVar are processed from beginning to end.
STEP -1
option. Note: minus one (-1) is the only supported STEP increment.
Example:
dim as CFArrayRef array
dim as CFStringRef string
array = fn ArrayWithObjects( @"One", @"Two", @"Three", @"Four", @"Five", NULL )
for string in array
NSLog(@"%@",string)
next
while
and do
statements provide other useful kinds of loop structures.