Skip to main content

This is documentation for Caché & Ensemble.

For information on converting to InterSystems IRISOpens in a new window, see the InterSystems IRIS Migration Guide and Migrating to InterSystems IRIS, both available on the WRC Distributions pageOpens in a new window (login required).

If Construct

You use the If construct in order to evaluate conditions, and make decisions about which code to run based on the conditions. A construct, as opposed to a simple command, consists of a combination of one or more arguments, command keywords, and code blocks. Code blocks are simply one or more lines of code contained in curly braces. There can be line breaks before and within the code blocks. The full syntax of the If construct is:

if condition { code } elseif condition {code} else {code}
Copy code to clipboard

ElseIf and Else are both optional, and there may be more than one ElseIf. We'll cover other constructs later.

The example below uses If inside the root routine:

root ; root for my favorite team
    read "Team: ", t
    if ( t = "METS" ) {
        write !, "Go METS!" }
    else {
        write !, "Boo ", t, "!" }
Copy code to clipboard

...and then tests it using the Terminal:

SAMPLES>do ^root
Team: METS
SAMPLES>do ^root
Copy code to clipboard
FeedbackOpens in a new window