| Lesson 4 -- Program Flow Control |
|---|
This lesson covers the following subjects:
Flow ControlWhen a macro is run the first code following the BEGIN keyword is executed and then the next line and the next, etc until the programs END keyword is reached. Now this may work fine for simple tasks but if you are to be able to write truly useful macros you must learn how to control the code that is executed. Controlling the lines of code that are run at any given time is called Flow Control. By using the methods of flow control covered in this lesson you will be able to control the actions that are taken by your macros. You will have tools that allow users to interact with your code and you will have much better macros than if you were to arrange all your code in order and let it simply run down the list.IF statements The IF statement is probably the simplest method of flow control. It works much as you would imagine. The syntax of the IF statement is
What this tells the macro is IF <evaluation> is true execute the
statements immediately following. The <evaluation> sequence must
evaluate to either TRUE or FALSE. There is no maybe, sometimes or kinda.
You can use AND, NOT and OR to build complex <evaluations> but we will
save that for future lessons. is better written as: Notice that in the evaluations we use [ = ] (equals sign) not [ := ] (colon/equals sign). [ = ] is the operator that evaluates a condition and [ := ] assigns a value. You can nest IF statements to your hearts content but be careful with the formatting or you will not be able to read your code. For example: Notice how I aligned the keywords for the different Statements. This is critical if you are to be able to read your code. One of the most common compiler errors is 'END expected' This error will tell you that you are missing an END keyword and this is usually because you lost track of the flow in a series of nested loops. It is possible to have none of the statements executed if none of the <expressions> evaluate to TRUE. |