Page 172 of 346 Start Back Next End ZBasic System Library 160 ZBasic Microcontrollers IIf Type Function returning the same type as the second parameter Invocation IIf(testExpr, trueExpr, falseExpr) Parameter Method Type Description testExpr ByVal Boolean The expression to evaluate, the result of which determine which expression value will be returned. trueExpr ByVal any type The value to return if testExpr evaluates to True. falseExpr ByVal any type The value to return if testExpr evaluates to False. Discussion This function is adapted from VB6 where it is sometimes called “Immediate If”. It is used to select one of two values based on the result of a test. Employing this function will generally result in less code than an equivalent If-Then-Else structure. On the other hand, the execution of this function does use more stack space than an equivalent If-Then-Else structure. Also, it is important to note that using this function is not exactly the same as an If-Then-Else because both the trueExpr and the falseExpr are always evaluated. This difference is only significant if the evaluation of one or both of these expressions has side effects. Note that trueExpr and falseExpr must have the same type or be of compatible types. Examples Dim a as Byte Dim b as UnsignedInteger Dim u as UnsignedInteger u = IIf(a > 3, 5, b) Debug.Print IIf(a = 5, "Hello", "Goodbye") Compatibility This function is not available in BasicX compatibility mode. Also, it is only supported by ZX firmware v1.1.0 or later.