Tivoli Service Desk 6.0 Developer's Toolkit Script Programming Guide

Chapter 3: Conditional and Control Structures

Back to Table of Contents


Introduction

This chapter covers the conditional and control constructs that handle testing and the response to the test conditions. These constructs also provide the means to alter program flow or to repeat actions as needed.

Conditional constructs

Developer's Toolkit provides two conditional constructs:

Control constructs

Developer's Toolkit provides several constructs for altering program flow:

The IF statement

IF statement form

The general form of the Developer's Toolkit IF statement is:

IF <condition> THEN 
  <statement-list> 
ELSIF <2nd condition> THEN 
  <statement-list> 
ELSE 
  <statement-list> 
END; 

ELSIF and ELSE statements are not required if only one condition is being tested.

IF example

Example uses of this construct are:

IF (age < 18) OR (age > 90) THEN 
  WRITELN('Improper age, please re-enter'); 
END; 
IF display = 'CGA' THEN 
  LoadCGADriver; 
ELSIF display = 'MCGA' THEN 
  LoadMCGADriver; 
ELSIF display = 'EGA' THEN 
  LoadEGADriver; 
ELSE 
  LoadVGADriver; 
END; 
IF processor = '8086' THEN 
  maxMem := 640000; 
  operatingSystem := 'DOS'; 
ELSE 
  maxMem := 16000000; 
  operatingSystem := 'OS/2'; 
(* operatingSystem is a string *) 
END;


True conditions

IF and ELSIF are always followed by a Boolean condition. If the condition evaluates to TRUE, the statements following the IF or ELSIF are executed.

False conditions

If the condition evaluates to FALSE, one of three things occurs:

Short-circuited Boolean evaluation

Developer's Toolkit uses short-circuited Boolean evaluation. This means that if you evaluate two clauses:


The WHEN Statement

WHEN statements are a special form of IF statements that allow you to perform an action based on the value of a variable or an expression.

IF statement example

Consider the following IF statement:

IF i*j = 1 THEN 
  WriteOne; 
ELSIF i*j = 2 THEN 
  WriteTwo; 
ELSIF i*j = 3 THEN 
  WriteThree; 
ELSE 
  WriteOther; 
END; 

In the preceding example, checking the value returned from the expression i*j involves recalculating the expression for each input value. If i*j is greater than 3, the expression i*j will be evaluated three times.

This method has two negative side effects:

An alternative method would be to assign the value of i*j to a local variable and then use that variable in the IF statement.

WHEN statement example

A better solution for both maintenance and performance is to use a WHEN statement instead of an IF statement.

The Developer's Toolkit WHEN statement provides a better way to evaluate a condition, as shown below.

WHEN i*j IS 1 THEN 
  WriteOne; 
ELSWHEN 2 THEN 
  WriteTwo; 
ELSWHEN 3 THEN 
  WriteThree; 
ELSE 
  WriteOther; 
END; 

The WHEN statement evaluates an expression once and then compares it to each of the test values. The expression must be of the simple data type integer or string.

Note: When used with strings, WHEN is case-insensitive.

You can also specify multiple test values by using the comma-delimited format shown in this example:

WHEN i*j IS 1,2,3 DO 
  WriteOne; 
ELSWHEN 4,5,6 DO 
  WriteTwo; 
ELSWHEN 7,8,9 DO 
  WriteThree; 
ELSE 
  WriteOther; 
END; 

FOR Loops

The FOR loop construct is used to execute one or more statements a predetermined number of times. The FOR construct takes the following form:

FOR <loop-parameter>:=<lo-bound> TO <hi-bound> DO 
  <statement-list> 
END; 

The constituents of this form are. shown in the table.

Constituent Description
<loop-parameter> An integer variable that is assigned to the values between <lo-bound> and <hi-bound> (inclusive)
<lo-bound> An integer expression that is evaluated once at the beginning of the loop and is assigned to the <loop-parameter>
<hi-bound> An integer expression that is evaluated once at the beginning of the loop and is compared to the value of <loop-parameter> each time it passes through the loop
<statement-list> One or more Developer's Toolkit statements that are executed for every value of <loop-parameter> between <lo-bound> and <hi-bound> (inclusive)

FOR example

The following example illustrates the simplest use of the FOR statement. In this example, a string is written to the display 10 times. The value of the i variable is not used within the statement list.

FOR i:=1 TO 10 DO 
  WinWriteLN(myWindow,'Hello'); 
END; 

The following example is slightly more complicated.

FOR i:=j TO k*3 DO 
  WinWriteLN(myWindow,'i = ' & i); 
END; 

Note: If the lower bound is equal to the upper bound, the statement list is executed one time. If the lower bound is greater than the upper bound, the statement list is not executed at all.

Using DOWNTO

The keyword DOWNTO can be used in place of TO in order to loop from a high value down to a low value as shown in the following example:

FOR i:=10 DOWNTO 1 DO 
  WinWriteLN(myWindow,i); 
END;


Using FOR in lists

The FOR statement has a second form for use with variables of type LIST:

FOR <list-parameter> DO 
  <statement-list> 
END; 

Here, the statement list is executed once for each member in the list parameter. The list parameter's current element pointer, $Current, is initialized to point to the first element in the list. At the end of execution of the statement list, the element pointer is advanced. This allows one or more statements to be performed for every element in a list.

Consider the following example:

ListInsert(myList,'One'); 
ListInsert(myList,'Two'); 
ListInsert(myList,'Three'); 
FOR myList DO 
  WinWriteLN(myWindow,myList[$Current]); 
END; 

REPEAT Loops

The REPEAT loop construct performs a set of statements once, and then continues to perform them until a certain condition becomes true.
The REPEAT loop construct has the following form:

REPEAT 
  <statement-list> 
UNTIL <condition>; 

The following code writes the string 'Hello' to the screen 10 times:

i:=1; 
REPEAT 
  WinWriteLN(myWindow,'Hello'); 
  i:=i + 1; 
UNTIL i > 10; 

Boolean expressions

In the previous example, <condition> is a Boolean expression that is evaluated each time through the loop. If it evaluates to FALSE, the statements in <statement-list> are executed again.
Boolean expressions can consist of clauses connected by:

A clause consists of two expressions of compatible data types separated by a relational operator (=, >, <, and so on).

WHILE Loops

The WHILE loop construct is similar to the REPEAT loop construct, except that the exit condition is tested at the beginning of the loop instead of at the end. The statements in a REPEAT loop construct are guaranteed to execute at least once, but the statements in a WHILE loop construct do not execute at all if the loop condition is false initially.

WHILE loop constructs have the following form:

WHILE <condition> DO 
  <statement-list> 
END; 

In the following program the string 'Hello' is written to the screen 10 times

i:=1; 
WHILE i <= 10 DO 
  WinWriteLN('Hello'); 
  i:=i + 1; 
END; 

Exit and the ExitLoop Statement

Exit

The Exit statement can be used to exit from a call to a function or a procedure. When issued, it causes the current routine to cease completely, even if it is placed inside a FOR, REPEAT, or WHILE loop.

ExitLoop

As its name implies, the ExitLoop statement can be used to exit a loop before the termination condition becomes true.

In the following example, a loop is set up to read and print every row from the EMPLOYEES database table. If the SQLFetch statement fails by returning a value less than 1 (thereby indicating either an error or the end of the result set), the ExitLoop statement causes the loop to cease execution. The statement immediately following the loop is the next one executed.

ExitLoop example

The following example illustrates the use of ExitLoop.

VARIABLES 
  i: Integer; 
  cursor: SQLCursor; 
  employee: EmployeeRecord; 
ACTIONS 
  i:=SQLSelect(cursor,'SELECT * FROM EMPLOYEES'); 
  WHILE i > 0 DO 
    i:=SQLFetch(cursor,employee); 
    IF i < 1 THEN 
      ExitLoop; 
    END; 
    PrintEmployee(employee); 
  END; 
  SQLCloseCursor(cursor);

Tivoli Service Desk 6.0 Developer's Toolkit Script Programming Guide

Back to Table of Contents

Copyright