Skip to main content

IF (legacy version)

Evaluates an expression, then selects which line of code to execute based on the truth value of the expression.

Synopsis

IF expression command1
ELSE command2

I expression command1
E command2

Arguments

Argument Description
expression Optional — An expression (or a comma-separated list of expressions) that evaluates to a boolean value.
command1 One or more Caché commands that are executed if expression evaluates to TRUE, or if expression is omitted.
ELSE command2 One or more Caché commands that are executed if expression evaluates to FALSE. The ELSE keyword is optional.

Description

Note:

This page describes the legacy version of the IF command. This version is considered legacy as of Caché 4.0, and should not be used in new programming. It is described here solely for compatibility with legacy applications.

The legacy IF command is line-oriented; commands to be executed must follow it on the same program line. Curly braces are not used and line formatting is restrictive. The new IF command is block structured; the block it executes consists of commands found within the curly braces that follow the IF command. Line formatting (white space, line breaks) is unrestrictive. The new version of IF does not use the $TEST special variable.

The old and new forms of IF and ELSE are syntactically different and should not be combined; therefore, an IF of one type should not be paired with an ELSE of the other type.

The IF command has two forms:

Without an argument

With an argument

IF Without an Argument

IF without an expression argument executes the commands that follow it on the same line if the current value of the $TEST variable is TRUE (nonzero). If $TEST is FALSE (0), Caché ignores the remaining commands on that line and continues execution with the next line (commonly the ELSE command). Thus, the ELSE keyword is equivalent to IF '$TEST (if $TEST is FALSE).

IF With an Argument

IF with an expression argument executes the commands that follow it on the same line if expression evaluates to TRUE (nonzero). (Caché also sets $TEST to TRUE.) If expression evaluates to FALSE, Caché sets $TEST to FALSE (0), ignores the remaining commands on that line, and continues execution with the next line.

The next line typically begins with an ELSE keyword, followed by one or more commands on the same line. The IF command does not require an ELSE keyword, but ELSE is required to specify an option that is only executed when IF is false. Always specifying ELSE is a recommended programming practice.

This line-oriented ELSE keyword can be abbreviated as E. (The newer block-oriented ELSE keyword cannot be abbreviated.) The old and new forms of IF and ELSE are syntactically different and should not be combined; therefore, an IF of one type should not be paired with an ELSE of the other type.

The expression argument can take the form of a single expression or a comma-separated list of expressions. For an expression list, Caché evaluates the individual expressions in left to right order. It stops evaluation if it encounters an expression that is FALSE. If all expressions evaluate to TRUE, Caché executes the command(s) following the expression argument on that line. If any expression evaluates to FALSE, Caché ignores any remaining expressions, and does not execute the commands on that line, but continues execution at the next line of code.

See Also

FeedbackOpens in a new tab