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 syntax of the If construct is:


if (condition_1) {code_1} elseif (condition_2) {code_2} ... elseif (condition_N-1) {code_N-1} else {code_N}

You can enclose conditions within parentheses, though they are not required. ElseIf and Else are both optional, and there may be more than one ElseIf.

The example below uses If inside the Root class method:

VS Code - ObjectScript


Class ObjectScript.Examples
{

/// root for my favorite team
ClassMethod Root()
{
    read "Team: ", t
    if (t = "") { quit }  // stop execution if no team is specified
    if (t = "METS") {
        write !, "Go METS!" }
    else {
        write !, "Boo ", t, "!" }
}
}
Testing using the Terminal


USER>do ##class(ObjectScript.Examples).Root()
Team: METS
Go METS!
USER>do ##class(ObjectScript.Examples).Root()
Team: BLUE SOX
Boo BLUE SOX!
USER>do ##class(ObjectScript.Examples).Root()
Team:
USER>
FeedbackOpens in a new tab