Skip to main content

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}

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, "!" }
    quit

...and then tests it using the Terminal:

SAMPLES>do ^root
Team: METS
Go METS!
SAMPLES>do ^root
Team: BLUE SOX
Boo BLUE SOX!
SAMPLES>
FeedbackOpens in a new tab