Lesson 4 -- Program Flow Control

This lesson covers the following subjects:

Flow Control

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.

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

ex4-1.gif (2868 bytes)
figure 4-1

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:

ex4-2a.gif (1914 bytes)
figure 4-2a

is better written as:


ex4-2b.gif (1946 bytes)
figure 4-2b

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:


ex4-3.gif (3425 bytes)
figure 4-3

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.

< Previous Lesson   |   Next Lesson >
DCAL Tutorial Contents

Thank you for printing this page. Please feel free to contact us for further assistance. You can call our sales department at +1 (800) 394-2231, Monday through Friday from 8:00 a.m. to 5:00 p.m. Eastern time or send an e-mail message to info@datacad.com.