Inline If Statements

From Sierra Wiki
Jump to navigationJump to search

Note: This article only applies to SCI Companion

The "if" statement does not return a value, making a the nesting of if's (inline) like this not possible:

Code:

<syntaxhighlight lang="sci" class="cs"> (if (> a ((if(> b c) 5)(else 9)) d) // This syntax works in SCI Studio, but will not compile in SCI Companion

 ...

) </syntaxhighlight>

As indicated above, this code will work in Studio, but will not work in Companion. The way to handle this in Companion is to implement a procedure, which will return a value:

Code:

<syntaxhighlight lang="sci" class="cs"> (procedure public (iif pCondition pTrue pFalse)

  (if (pCondition)
    return pTrue
  )(else 
    return pFalse 
  )

)</syntaxhighlight>

The original statement can now be re-written as:

Code:

<syntaxhighlight lang="sci" class="cs"> (if (> a iif(> b c 5 9) d)

 ...

) </syntaxhighlight>


References



Also See