Previous pageNext pageTable of Contents Home page

CorelSCRIPT flow control

CorelSCRIPT allows you to create not only simple scripts. You can write a program that runs differently depending on some conditions, for example, user input, the system state or the result of some operation. There are a number of script commands that can result in execution branching, i.e. changing the script flow and redirecting it to some other part of the script code. For example, you can program the following situation: if the user presses Enter, then open the selected file or otherwise terminate the script. This is the simple condition execution.

Another example of non-straight execution is: select next object in the drawing, fill it with red and move it by 2 inches to the right. Repeat the same steps 10 times (or repeat the same steps until you reach the first selected object). This is an example of a program loop.


Conditional statements

CorelSCRIPT has several commands that can be used to control the script execution depending on some condition.

IF...THEN...ELSE

IF...THEN...ELSE statement is used to test a condition and execute one part of the code if the condition is true or another if it is false. The syntax of this statement is the following:

IF condition THEN
	A list of commands to be executed if the condition is true
ELSE
	A list of commands to be executed if the condition is false
ENDIF

Note that ENDIF can be spelled in two ways: ENDIF and END IF

If not necessary, the ELSE statement and its corresponding list of commands can be omitted.

If the list of commands to be executed contains only one command the IF...THEN...ELSE statement can have one of the following formats:

IF condition THEN command1 ELSE command2
IF condition THEN command

The condition is a logical expression. A simple logical expression is comprised of a relational operator and its two operands. The relational operators are:

Operator Meaning
> Greater than
< Less than
= Equal to
=> or >= Greater than or equal to
=< or <= Less than or equal to
<> Not equal to

A few examples:

IF a%=0 THEN ... ' if the value of a is equal to 0 then ...
IF Width&>1000 THEN ... ' if the width is greater than 1000 then...

Simple logical expressions can be combined with the words AND, OR, NOT to create more complex expressions. You can use parentheses to control the evaluation of the expression:

IF Width&<=1000 AND Width&>=500 THEN ... ' if the width ranges from 500 to 1000 then...
IF Height&*Width&>100 and 2*(Width&+Height&)>60 THEN ... ' if the area of an rectangle is greater than 100 and its perimeter is less than 60 then...

SELECT CASE

SELECT CASE statement is usually used if there are many choices. For example, if a variable value is equal to 1 then do something, if it is 2 then do something else, if 3, then do another action and so on. The statement format is:

SELECT CASE testcondition
	CASE case1
		A list of commands to be executed
	CASE case2
		A list of commands to be executed
	CASE case3
		A list of commands to be executed
	...
	CASE ELSE
		A list of commands to be executed
END SELECT

Here testcondition is a value being tested. This value can be any numeric or string expression. Case1, case2, case3,... are the conditions for the value being tested. If the condition is true, the commands right below the CASE statement are executed. If it is false, the next condition is evaluated. If no condition turns out to be true, then the commands below CASE ELSE statement are executed. However, the CASE ELSE statement may be omitted. In this case, the script execution continues at the command that immediately follows the END SELECT statement.

The conditions in the SELECT CASE construction can be one of the following:

Condition When is evaluated to true Example
value if the value is equal to the testcondition 2
value1, value2, ... if any of listed values are equal to the testcondition 1, 7, 5
value1 TO value2 if the testcondition ranges from value1 to value2 inclusive 5 TO 30
IS relation if the relational result is true IS >=50

An example of SELECT CASE:

SELECT CASE choice%
	CASE 0 ' the integer variable choice equals to 0
		MESSAGE "Zero" ' Show a message
	CASE IS <0 ' if choice is less than 0
		MESSAGE "choice is less than zero"
	CASE 1, 2, 3
		MESSAGE "choice equals to 1, 2 or 3"
	CASE 4 TO 7, 10 TO 20, IS >30
		MESSAGE "choice ranges from 4 to 7, or from 10 to 20 or is greater than 30"
	CASE ELSE
		BEEP ' generate a short sound
		MESSAGE "no condition evaluates to true"
END SELECT

Looping statements

To cycle the script execution through some part of the code you should use the looping statements. They allow you to specify the number of times that part should be executed or a condition while the code is executed. The code that should be executed several times is called the body of a loop.

FOR...NEXT

This statement is used for specifying the particular number of times to repeat its body. This statement has the following syntax:

FOR counter=start TO end STEP increment
	A set of commands to be executed
NEXT counter

Counter is a variable name that is used as the loop counter. Its value changes as the loop repeats its body.

FOR...NEXT statement works to the following scheme. At the beginning of the loop the variable counter is assigned a value of start. Then the body of the loop is executed. At the NEXT statement the counter is increased by increment value. If STEP increment part of the statement is omitted then increment is assumed to be 1. The body of the loop is executed until counter becomes equal to or greater than end.

If increment is negative than this value is subtracted and the loop executes until counter becomes equal to or less than end.

Some examples for FOR...NEXT statement:

DIM n(10) as integer
FOR i%=1 to 10
  n(i)=i
NEXT i

This example declares an array of 10 integer elements and assigns each of them their ordinal numbers, i.e. the first elements is assigned the value of 1, the second - 2 and so on. Don't forget that introducing a variable for the first time you should declare it either implicitly or explicitly. In this example, the array n is explicitly declared as being integer. The loop counter i is also integer because it is implicitly declared with the '%' character. To tell the truth, if you don't declare a variable in either way, it won't be an error. But this variable will be of variant type that takes more memory and all operations with it are performed slower. So, you'd better declare a variable prior to using it.

Another example:

WITHOBJECT "CorelDraw.Automation.7"
FOR r&=10000 to 50000 step 5000
   .CreateEllipse r,-r,-r,r,0,0,FALSE
NEXT r
END WITHOBJECT

This script creates 9 concentric circles with increasing radius from 1 cm to 5 cm with the step of 5 mm. Please note, that integer can't hold numbers greater than 32767, therefore the variable r was declared as long ('&' character).

Some basic application-specific commands (like CreateEllipse) will be described later.

WHILE...WEND

WHILE...WEND statement is a simple conditional loop. It means that the loop continues to execute as long as some condition is true. The syntax of the statement is the following:

WHILE condition
	The body of the loop
WEND

The condition is a Boolean expression specifying how long the loop will be executed. This condition has the same structure as in IF...THEN...ELSE command. For example:

i%=0
WHILE i<10
   i=i+1
WEND

Here an integer variable i is assigned the value of 0 at the beginning. Then its value is incremented until it reaches 10. If the condition turns out to be false the loop body is skipped and execution continues at the next instruction following WEND statement. Note that if the condition is false the very first time, the loop body is not executed at all.

You can use AND, OR, NOT keywords to build complex conditions:

a$=""
WHILE a$<>"exit" AND a$<>"quit"
   a$=INPUTBOX("Enter a command (exit or quit to finish the script)")
   SELECT CASE a$
	CASE "greet"  ' if a$ is equal to "greet"
		MESSAGE "Greeting to you"
	CASE "exit","quit" ' if a$ equals to "exit" or "quit"
		MESSAGE "Exiting..."
	CASE ELSE ' Otherwise.
		MESSAGE "You entered: "+a$
   END SELECT
WEND

This script prompts for a word. If this word is "greet", the message box is displayed. If it is either "exit" or "quit", the script displays a message "Exiting..." and stops execution. If any other word is entered, the script simply says "You entered: your word here". The main idea is that the script loops until you enter "exit" or "quit". Here a new keyword is used - INPUTBOX. It simply displays a message and lets the user enter text that is returned by the procedure.

SELECT CASE construction is used here for multiple condition evaluation. It tests the value of variable a$ and executes different parts of the script depending on it. These parts are located under the corresponding CASE statements.

DO...LOOP

This is an extended WHILE...WEND loop. It can be used for conditions like "execute while the condition is true" or "execute until the condition becomes true". Besides, it can test the condition before or after the loop body is executed. The four types of DO...LOOP statement have the following syntax:

DO WHILE condition
   Loop body
LOOP
DO UNTIL condition
   Loop body
LOOP
DO
   Loop body
LOOP WHILE condition
DO
   Loop body
LOOP UNTIL condition

Evidently, DO WHILE...LOOP is an equivalent of WHILE...WEND loop.

Some examples:

DIM arr&(10)
index%=1
s%=0
DO WHILE index%<=10
  s=s+arr(index)
  index=index+1
LOOP

Here the loop is used to sum up all 10 elements of array arr.

DO
  reply$=INPUTBOX("Enter 'yes' or 'no'")
LOOP UNTIL reply="yes" OR reply="no"

The loop cycles until the user enter either "yes" or "no".


Terminating loops

Sometimes it is necessary to exit the loop before it ends. For example you are to create 10 files on your hard disk. While writing the fifth file the disk becomes full. An error message should be issued and the loop must be terminated. For this purpose you should use EXIT keyword. Depending on the type of the loop the EXIT command is followed by an appropriate statement. It could be:

EXIT FOR for terminating FOR...NEXT loop

EXIT WHILE for terminating WHILE...WEND loop

EXIT DO for terminating DO...LOOP loop

Example:

FOR i%=1 to 10
  a$=INPUTBOX("Enter 'exit' to terminate the loop")
  IF a$="exit" THEN EXIT FOR
NEXT i

This example loops ten times displaying a dialog box. If you enter "exit", the script terminates immediately.


Labels and jumps

Sometimes it is difficult to build an efficient code execution using only conditional statements. You can use jump commands to change the execution point directly. To mark a place in the script where the execution should be passed you can label it. A label is a word which should stand at the beginning of a line and is followed by a colon (:). The name of a label must be built the same way as that of variables - it can contain letters from 'A' to 'Z' and from 'a' to 'z', numbers '0'-'9' and the underscore character '_' but must not start with a number. In addition it should not be a reserved word (CorelSCRIPT command). The label name is not case sensitive. Here is an example of labeled lines:

Label_1:
  Message "This is an example"
Label_2: IF k%=3 THEN MESSAGE "k equals to 3"

A label must not be preceded by spaces or tabs, it should begin a line. You can place CorelSCRIPT instructions right after a label or on a separate line.

To go to a line marked with a label you should use GOTO statement. It should be followed with a label name where to go to, but the label name should not be followed by the colon here:

IF k>3 THEN GOTO greater
MESSAGE "K is not greater than 3"
GOTO proceed
Greater: MESSAGE "K is greater than 3"
proceed:

There is one more jump command - GOSUB. It acts much the same as GOTO, but it remembers the place where it is issued and the execution returns at this point when RETURN command appears. This is very useful for reusing some part of a script code. You can move some common script instructions to the end of the script and label its beginning. At the end of the excerpt you place the RETURN command. Each time you need to execute that set of instructions just include GOSUB command:

Msg$="Enter your name" ' the message to show in subroutine GetUserInput
GOSUB GetUserInput
Name$=Reply$ ' Checkpoint #1
Msg$="Enter your age"
GOSUB GetUserInput
Age$=Reply$ ' Checkpoint #2
'... The rest of a program

GetUserInput:
Reply$=INPUTBOX(Msg$)
IF Reply$="" THEN
  MESSAGE "You should enter something"
  GOTO GetUserInput
ENDIF
RETURN

In this example, subroutine GetUserInput prompts user to enter a string and test if the string entered contains any character. The message shown by INPUTBOX is stored in variable Msg. This allows you to use one INPUTBOX statement for displaying two different prompts. First time "Enter your name" is assigned to Msg prior to calling the subroutine. In GetUserInput INPUTBOX command displays the prompt stored in variable Msg and waits for user to enter the data. The string entered is assigned to variable Reply (note once again the implicit data type declaration with the '$' character which means that variable Reply is of a string type).

The next step is to test if the user entered something. If he has entered nothing or simply pressed Cancel button, INPUTBOX returns with a special string value - an empty string, i.e. a string containing no characters at all. The IF statement tests whether the string has no characters and if so, the message is issued and execution is directed back to GetUserInput label with GOTO statement. In this case the prompt will be shown again.

If the user enters some data, the script continues running and returns from the subroutine when it reaches RETURN statement. It returns to the command following calling GOSUB. These commands are marked as Checkpoint #1 and Checkpoint #2 in the example.

After execution returns from the subroutine, variable Reply contains the data the user entered. You can do anything you like with it. In the example, the first entered value is reassigned to variable Name to prevent it from being erased by the second call to the subroutine.

Though GOTO and GOSUB are convenient commands, they can make your script difficult to read and locate malfunctions. Consider using conditional or looping statements if possible. CorelSCRIPT has more structural and convenient commands for subroutines which will be discussed on the next page. These commands should be used instead of GOSUB.


What next?

On the next page you will learn about Corel SCRIPT structure, procedures and functions.


Previous pageNext pageTable of Contents Home page