式
式とは、自由形式の構文を使用してプログラム論理を表す方法です。式を使用すると、固定形式ステートメントより読みやすく、簡潔な方法 でプログラム・ステートメントを作成することができます。
式は、オペランドと演算子の単純なグループです。 たとえば、以下が有効な式です。
A+B*21
STRINGA + STRINGB
D = %ELEM(ARRAYNAME)
*IN01 OR (BALANCE > LIMIT)
SUM + TOTAL(ARRAY:%ELEM(ARRAY))
'The tax rate is ' + %editc(tax : 'A') + '%.'
式は、次のステートメントでコーディングされる場合があります。
- CALLP (プロトタイプ・プロシージャーまたはプログラムの呼び出し)
- CHAIN (ファイルからのランダム検索)(自由形式演算のみ)
- CLEAR (消去)(自由形式演算のみ)
- DELETE (レコードの削除)(自由形式演算のみ)
- DSPLY (メッセージ表示)(自由形式演算のみ)
- DOU (条件が真になるまでの繰り返し)
- DOW (条件が真の間繰り返し)
- EVAL (式の評価)
- EVALR (式の評価、右寄せ)
- EVAL-CORR (対応するサブフィールドの代入)
- FOR (For)
- IF (If)
- RETURN (呼び出し元への戻し)
- READE (等しいキーのレコードの読み取り)(自由形式演算のみ)
- READPE (等しいキーの前のレコードの読み取り)(自由形式演算のみ)
- SETGT (より大きい設定)(自由形式演算のみ)
- SETLL (下限の設定)(自由形式演算のみ)
- SORTA (配列の分類)
- WHEN (真の場合に選択)
- XML-INTO (XML 文書の変数への構文解析)
- XML-SAX (XML 文書の構文解析)
図 1 では、式の使用例をいくつか示しています。
図 1. 式の例
*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
* The operations within the DOU group will iterate until the
* logical expression is true. That is, either COUNTER is less
* than MAXITEMS or indicator 03 is on.
/FREE
dou counter < MAXITEMS or *in03;
enddo;
// The operations controlled by the IF operation will occur if
// DUEDATE (a date variable) is an earlier date than
// December 31, 1994.
if DueDate < D'12-31-94';
endif;
// In this numeric expression, COUNTER is assigned the value
// of COUNTER plus 1.
Counter = Counter + 1;
// This numeric expression uses a built-in function to assign the numb
// of elements in the array ARRAY to the variable ARRAYSIZE.
ArraySize = %elem (Array);
// This expression calculates interest and performs half adjusting on
// the result which is placed in the variable INTEREST.
eval(h) Interest = Balance * Rate;
// This character expression builds a sentence from a name and a
// number using concatentation. You can use built-in function
// %CHAR, %EDITC, %EDITW or %EDITFLT to convert the numeric value
// to character data.
// This statement produces 'Id number for John Smith is 231 364'
String = 'Id number for '
+ %trimr (First) + ' ' + %trimr (Last)
+ ' is ' + %editw (IdNum: ' & ');
// This expression adds a duration of 10 days to a date.
DueDate = OriginalDate + %days(10);
// This expression determines the difference in seconds between
// two time values.
Seconds = %diff (CompleteTime: t'09:00:00': *seconds);
// This expression combines a date value and a time value into a
// timestamp value.
TimeStamp = TransactionDate + TransactionTime;
/END-FREE