manual:chapter5:basics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
manual:chapter5:basics [2021/09/15 07:20]
claudio [Flow control]
manual:chapter5:basics [2021/09/15 10:32]
claudio
Line 389: Line 389:
 will leave the numbers 1,3,5,7,9 on the stack. will leave the numbers 1,3,5,7,9 on the stack.
  
-===== Ending a FOR loop ===== 
  
-The end of the loop condition is determined by the initial start and end values. If the end value is higher than the start value (therefore counting up), the loop will continue while the variable is less than or equal to the end value. On the contrary, if the end value is lower than the start value, the loop will continue as long as the variable is greater than or equal to the end value.+The behavior of the ''NEXT'' statement is different from the ''STEP'' statement: 
 +  * ''NEXT'' always increments the counter by 1, while ''STEP'' could go either direction or leave the same value. 
 +  * ''NEXT'' always checks if the counter is ≤ end value to continue the loop. ''STEP'' checks if the counter is ≤ or ≥ end value depending on the direction of the loop
 + 
 +When using ''STEP'', the direction of a loop is determined by the initial start and end values. If the end value is higher than the start value (therefore counting up), the loop will continue while the variable is less than or equal to the end value. On the contrary, if the end value is lower than the start value, the loop will continue as long as the variable is greater than or equal to the end value.
  
 When ''STEP'' is used, the magnitude and direction of the step do not affect the condition to end the loop. For example: When ''STEP'' is used, the magnitude and direction of the step do not affect the condition to end the loop. For example:
Line 405: Line 408:
 </file> </file>
  
-In the above example ''J'' will count from 1 to 10 with a step ''S''. The loop begins with a step of 1, being stored in ''S''. The loop will run leaving the values of J in the stack, initially 1,2,3,4 then the condition in the ''IF'' statement becomes true and the step changes to -1. +In the above example ''J'' will count from 1 to 10 (therefore this is a loop counting up) with a step ''S''. The loop begins with a step of 1, being stored in ''S''. The loop will run leaving the values of J in the stack, initially 1,2,3,4 then the condition in the ''IF'' statement becomes true and the step changes to -1. 
-The loop will now begin counting down 3,2,1 and the second ''IF'' statement becomes true, changing the step to 2. The loop will continue running 3,5,7,9, and the value of 11 will exit the loop and +The loop will now begin counting down 3,2,1 and the second ''IF'' statement becomes true, changing the step to 2. The loop will continue running 3,5,7,9, and then the loop will exit when the counter is >10. The complete sequence generated by the code above is then 1,2,3,4,3,2,1,3,5,7,9. 
 + 
 +When ''NEXT'' is used, the direction is always assumed to be up regardless of start and end values, therefore the following loop: 
 + 
 +<file> 
 +« A 10 FOR J J NEXT » 
 +</file> 
 + 
 +Will produce the following outputs, depending on the value of ''A'': 
 + 
 +  * ''A=1'' will produce 1,2,3,4,5,6,7,8,9,10 
 +  * ''A=10'' it will produce 10 
 +  * ''A=11'' it will produce 11 
 + 
 +Notice that despite the starting value being higher than the end value and the loop using ''NEXT'' (therefore it's a assumed to be a loop counting up), the loop executed at least once. This is because the decision to exit the loop is made when ''NEXT'' is reached. 
 + 
 +Now let's review a loop using ''STEP'' with a positive step: 
 + 
 +<file> 
 +« A 10 FOR J J 1 STEP » 
 +</file> 
 + 
 +Will produce the following outputs, depending on the value of ''A'': 
 + 
 +  * ''A=1'' will produce 1,2,3,4,5,6,7,8,9,10 
 +  * ''A=10'' it will produce 10 
 +  * ''A=11'' it will produce 11,12,13,14,15,... infinite loop 
 + 
 +Notice that while the loop appears to count up by looking at the code, the condition A>10 causes the loop to be counting down because the start value is larger than the end value, and therefore it will only end when J<10, regardless of the magnitude and direction of the step. Since the step increases the counter, the loop never ends. 
 + 
 +In some situations it is desirable to keep the direction of the loop fixed regardless of the start/end values. The ''FORUP'' and ''FORDN'' commands can be used in lieu of ''FOR''. These two variants have two advantages: 
 +  * The direction of the loop is fixed and known, regardless of the values the limits might have at run time. 
 +  * The loop has one additional check when the word ''FORUP'' or ''FORDN'' is executed. This allows for early exit without being forced to execute the loop clause. 
 + 
 +Back to the example used above for ''NEXT'' but in this case using FORUP: 
 + 
 +<file> 
 +« A 10 FORUP J J NEXT » 
 +</file> 
 + 
 +Will produce the following outputs, depending on the value of ''A'': 
 + 
 +  * ''A=1'' will produce 1,2,3,4,5,6,7,8,9,10 
 +  * ''A=10'' it will produce 10 
 +  * ''A=11'' it will not produce any output 
 + 
 +It differs from using ''FOR'' in the last case: the clause between ''FORUP'' and ''NEXT'' is never executed because the starting value was already larger than the end limit. 
 + 
 +On the other end: 
 +<file> 
 +« A 10 FORDN J J -1 STEP » 
 +</file> 
 + 
 +  * ''A=1'' will produce no output 
 +  * ''A=10'' it will produce 10 
 +  * ''A=11'' will produce 11,10 
 + 
 +Note that ''NEXT'' always considers the loop direction going up, therefore ''NEXT'' is not compatible with ''FORDN'' which contradicts that assumption. 
 + 
 +Summarizing, the main differences between all the ''FOR'' constructs: 
 + 
 +     * ''FOR'' may count up or down, and may be used with both ''NEXT'' and ''STEP''. The loop clause is always executed at least once. 
 +     * ''FORUP'' always counts up, may be used with ''NEXT'' or ''STEP''. The loop clause is not executed if the starting value is greater than the end value. 
 +     * ''FORDN'' always counts down, can only be used with ''STEP''. The loop clause is not executed if the starting value is less than the end value. 
  
 + 
 === DO loops === === DO loops ===
 The syntax for this construct is: The syntax for this construct is:
  • manual/chapter5/basics.txt
  • Last modified: 2021/09/15 10:32
  • by claudio