FOR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The FOR statement creates a counter loop using specified start and stop numerical boundaries. The default increment is + 1. {{PageSyntax}} : FOR {{Parameter|counterVariable}} = {{Parameter|startValue}} TO {{Parameter|stopValue}} [{{KW|STEP}} {{Parameter|increment}}] :: ''{code}'' :: ⋮ : NEXT [{{Parameter|counterVariable}}] {{Parameters}} * The FOR {{Parameter|counterVariable}} name is required to define the counter span and may also be used aft...")
 
No edit summary
Line 2: Line 2:




{{PageSyntax}}  
{{PageSyntax}}
: [[FOR]] {{Parameter|counterVariable}} = {{Parameter|startValue}} [[TO]] {{Parameter|stopValue}} [{{KW|STEP}} {{Parameter|increment}}]
: [[FOR]] {{Parameter|counterVariable}} = {{Parameter|startValue}} [[TO]] {{Parameter|stopValue}} [{{KW|STEP}} {{Parameter|increment}}]
:: ''{code}''
:: ''{code}''
Line 13: Line 13:
* The {{Parameter|startValue}} [[TO]] {{Parameter|stopValue}} can be any literal or variable numerical type. Both values are  required.
* The {{Parameter|startValue}} [[TO]] {{Parameter|stopValue}} can be any literal or variable numerical type. Both values are  required.
* [[STEP]] can be used for a loop {{Parameter|increment}} other than the default ''plus 1 and can be any positive or negative literal or variable numerical value as long as the STEP value corresponds to the loop's {{Parameter|startValue}} and {{Parameter|stopValue}}.
* [[STEP]] can be used for a loop {{Parameter|increment}} other than the default ''plus 1 and can be any positive or negative literal or variable numerical value as long as the STEP value corresponds to the loop's {{Parameter|startValue}} and {{Parameter|stopValue}}.
* [[NEXT]] ends the [[FOR]] loop code block and increments the counter to the next value even when it exceeds the stop limit.  
* [[NEXT]] ends the [[FOR]] loop code block and increments the counter to the next value even when it exceeds the stop limit.




{{PageDescription}}
{{PageDescription}}
* [[FOR...NEXT]] counter loops must be within the proper start, stop and increment values or the entire loop code block will not be executed.  
* [[FOR...NEXT]] counter loops must be within the proper start, stop and increment values or the entire loop code block will not be executed.
* Avoid changing the FOR {{Parameter|counterVariable}}'s value inside of the loop. This obfuscates code and is a poor programming practice.
* Avoid changing the FOR {{Parameter|counterVariable}}'s value inside of the loop. This obfuscates code and is a poor programming practice.
* Once the loop has been started, changing the variables holding the {{Parameter|startValue}}, {{Parameter|stopValue}} or {{Parameter|increment}} value will not affect loop execution.
* Once the loop has been started, changing the variables holding the {{Parameter|startValue}}, {{Parameter|stopValue}} or {{Parameter|increment}} value will not affect loop execution.
Line 25: Line 25:
** The [[STEP]] {{Parameter|increment}} value cannot be changed inside of the loop.
** The [[STEP]] {{Parameter|increment}} value cannot be changed inside of the loop.
* Use '''[[EXIT]] FOR''' to leave a FOR loop early when a certain condition is met inside of the loop.
* Use '''[[EXIT]] FOR''' to leave a FOR loop early when a certain condition is met inside of the loop.
* The [[NEXT]] counter variable name is not required. NEXT loop increments can be separated by colons in nested FOR loops.  
* The [[NEXT]] counter variable name is not required. NEXT loop increments can be separated by colons in nested FOR loops.
* '''NOTE: When the FOR loop is exited after the {{Parameter|stopValue}} is reached, the {{Parameter|counterVariable}}'s value will be {{Parameter|stopValue}} + 1 (or {{Parameter|stopValue}} + {{Parameter|increment}})
* '''NOTE: When the FOR loop is exited after the {{Parameter|stopValue}} is reached, the {{Parameter|counterVariable}}'s value will be {{Parameter|stopValue}} + 1 (or {{Parameter|stopValue}} + {{Parameter|increment}})
* '''Beware of FOR loop counts that exceed the {{Parameter|counterVariable}} type limits and may repeat without error in QB64.'''
* '''Beware of FOR loop counts that exceed the {{Parameter|counterVariable}} type limits and may repeat without error in QB64.'''
Line 33: Line 33:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Adding all of the even numbers from 10 to 0.
''Example 1:'' Adding all of the even numbers from 10 to 0.
{{CodeStart}} '' ''
{{CodeStart}}
FOR i = 10 TO 0 {{Cl|STEP}} -2
FOR i = 10 TO 0 {{Cl|STEP}} -2
   totaleven% = i + totaleven%
   totaleven% = i + totaleven%
   PRINT totaleven%;
   PRINT totaleven%;
NEXT
NEXT
PRINT "After loop, i ="; i '' ''
PRINT "After loop, i ="; i
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}10 18 24 28 30 30 After loop, i = -2
{{OutputStart}}10 18 24 28 30 30 After loop, i = -2
Line 46: Line 46:


''Example 2:'' How an entire FOR loop block is ignored when the start and stop limits do not match the default or [[STEP]] increment.
''Example 2:'' How an entire FOR loop block is ignored when the start and stop limits do not match the default or [[STEP]] increment.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|PRINT}} "hi"
{{Cl|PRINT}} "hi"


Line 60: Line 60:


<!-- removed redundant example as Example 2 above shows exactly the same technique
<!-- removed redundant example as Example 2 above shows exactly the same technique
''See Example:''  
''See Example:''
* [http://qb64.net/wiki/index.php?title=Controller_Devices#Example Example that shows how ignoring bad FOR loops can work to a program's advantage without errors.] -->
* [http://qb64.net/wiki/index.php?title=Controller_Devices#Example Example that shows how ignoring bad FOR loops can work to a program's advantage without errors.] -->




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[STEP]]  
* [[STEP]]
* [[DO...LOOP]], [[WHILE...WEND]]
* [[DO...LOOP]], [[WHILE...WEND]]




{{PageNavigation}}
{{PageNavigation}}

Revision as of 01:40, 23 January 2023

The FOR statement creates a counter loop using specified start and stop numerical boundaries. The default increment is + 1.


Syntax

FOR counterVariable = startValue TO stopValue [[[:Template:KW]] increment]
{code}
NEXT [counterVariable]


Template:Parameters

  • The FOR counterVariable name is required to define the counter span and may also be used after the NEXT keyword.
  • The startValue TO stopValue can be any literal or variable numerical type. Both values are required.
  • STEP can be used for a loop increment other than the default plus 1 and can be any positive or negative literal or variable numerical value as long as the STEP value corresponds to the loop's startValue and stopValue.
  • NEXT ends the FOR loop code block and increments the counter to the next value even when it exceeds the stop limit.


Description

  • FOR...NEXT counter loops must be within the proper start, stop and increment values or the entire loop code block will not be executed.
  • Avoid changing the FOR counterVariable's value inside of the loop. This obfuscates code and is a poor programming practice.
  • Once the loop has been started, changing the variables holding the startValue, stopValue or increment value will not affect loop execution.
  • If the STEP increment value does not match the startValue TO stopValue the FOR loop block will be ignored.
    • If startValue is less than stopValue, use the default increment or positive STEP value or the loop will not be executed.
    • If startValue is more than stopValue, use a negative STEP interval or the loop will not be executed.
    • The STEP increment value cannot be changed inside of the loop.
  • Use EXIT FOR to leave a FOR loop early when a certain condition is met inside of the loop.
  • The NEXT counter variable name is not required. NEXT loop increments can be separated by colons in nested FOR loops.
  • NOTE: When the FOR loop is exited after the stopValue is reached, the counterVariables value will be stopValue + 1 (or stopValue + increment)
  • Beware of FOR loop counts that exceed the counterVariable type limits and may repeat without error in QB64.
    • For example, if counterVariable is of type INTEGER and the stop limit exceeds 32767, the counterVariable will reset back to -32768 and loop endlessly.


Examples

Example 1: Adding all of the even numbers from 10 to 0.

FOR i = 10 TO 0 STEP -2
  totaleven% = i + totaleven%
  PRINT totaleven%;
NEXT
PRINT "After loop, i ="; i
10 18 24 28 30 30 After loop, i = -2
Explanation: The loop counts down from 10 to every even value below it. The counter keeps stepping down until the FOR stop limit is reached or exceeded. Note that the value of i is -2 after the loop is exited. NEXT always increments the counter one last time.


Example 2: How an entire FOR loop block is ignored when the start and stop limits do not match the default or STEP increment.

PRINT "hi"

FOR i = 10 TO 1 'requires a negative STEP value
  PRINT "lo"
NEXT

PRINT "bye"
hi
bye 



See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage