This lesson covers the following subjects:
When 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.
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.
The optional ELSIF allows you to test a number of different possible <evaluations>. You can have an unlimited number of ELSIF's in your IF statement. The use of ELSIF is optional. If the first IF <evaluation> and any subsequent ELSIF <evaluations> are not true then the program will execute the statements following the last ELSE. The ELSE is optional. You may only have one ELSE keyword in your IF statement. An END keyword tells the compiler that this IF statement stops here.
Notice the use of the semi-colon in figure 4-1. All the <statement> lines must end in semi-colons (unless they are in themselves other flow control statements that don't require semi-colons). The IF statement itself doesn't have a semi-colon until the last END keyword. Use indentation to align the matching IF, ELSIF, ELSE and END keywords. This makes it much easier to read your code. Indent all the statements under the IF, ELSE or ELSIF keywords. This helps you follow what statements are being executed.
Use ELSIF rather than a series of IF...END statements. For example:
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.