-
Notifications
You must be signed in to change notification settings - Fork 2
3.4 Condition and Iteration
As you would expect, dflat has the usual ability to test for conditions and execute one flow or another. The options available are:
10 if <condition>20 else30 elif <condition>40 endif
In the above is any statement which evaluates to a number - which can be the usual comparison operators but also just a calculation. All conditional tests by dflat use 0 as false and non-zero as true in the flow change decision.
dflat allows for multi-line and statement flows, by simply following these rules;
- if a condition is met then execute all statements until the corresponding else, elif or endif is found
- if a condition is not met then find the corresponding else or elif and if found, execute all statements until a corresponding elif or endif
- continue execution from the statement after endif
- here the term 'corresponding' refers to the same nesting level as conditional statements can be nested
Single line example;
100 if a>b:println "greater":else:println "not greater":endif
Multiple line example;
200 if a>b210 println "greater"220 elif a<b230 println "less"240 else250 println "equal"260 endif
For iteration, dflat provides for, repeat and while constructs.
A for loop sets the loop variable to the start value and executes all statements until the matching next keyword. At that point the step value is added to the loop variable and then compared against the end value. If the step value is positive and the loop variable is greater than the end value, or if the step value is negative and the loop variable is less than the end value, iteration will stop, else continue from the statement after the for keyword.
For example to count from 0 to 1000 (note that the final value of a is 1001)
100 for a=0,1000,1110 println a120 next
An example to count from 1000 to 0 (note the final value of a is -1)
100 for a=1000,0,-1110 println a120 next
It should be noted that the end and step values are evaluated once and stored - they are not re-evaluated each time around the loop.
The while..wend construct works as follows; when dflat encounters a while, it evaluates the expression and if true (i.e. non-zero) executes the block of code until the matching wend, at which point it starts at the while statement again e.g. to count from 1 to 1000;
100 a=1110 while a<1000110 a=a+1120 wend
The repeat..until construct works as follows; when dflat encounters the repeat keyword, it executes the code block until it reaches a matching until keyword, at which point it evaluates the expression and if false (i.e. zero) continues execution from the statement after the repeat keyword e.g. to count from 1 to 1000
100 a=1110 repeat110 a=a+1120 until a>1000