Repository navigation
Iif
Home › Miscellaneous Keywords › IIf
Conditional function that returns one of two values.
IIf ( condition, expr_if_true, expr_if_false )conditionexpr_if_trueconditionexpr_if_falseconditionexpr_if_true
IIf returns a different numeric or string or UDT value (not a reference) depending of the result of a conditional expression evaluated at run-time.
Its typical use is in the middle of an expression; it avoids splitting it to put a conditional in the middle.
IIf only evaluates the expression that it needs to return. This saves time, and can also be useful to prevent evaluating expressions that might be invalid depending on the condition.
When IIf treats expressions of mixed numeric types (conditional expression evaluated at run-time):
Note: If the condition evaluation can be done at compile time (if the comparison expression is a constant), the usage rules are a bit more flexible and IIf can for example return a reference or even return a function call (but not a sub call).
if condition is non-zero, expr_if_true, otherwise expr_if_false
- Not available in the -lang qb dialect unless referenced with the alias
__Iif.
- New to FreeBASIC
Dim As Integer a, b, x, y, z
a = (x + y + IIf(b > 0, 4, 7)) \ zis equivalent to:
Dim As Integer a, b, x, y, z, temp
If b > 0 Then temp = 4 Else temp = 7
a = (x + y + temp) \ zDim As Integer I
I = -10
Print I, IIf(I>0, "positive", IIf(I=0, "null", "negative"))
I = 0
Print I, IIf(I>0, "positive", IIf(I=0, "null", "negative"))
I = 10
Print I, IIf(I>0, "positive", IIf(I=0, "null", "negative"))
SleepType UDT1
Dim As Integer I1
End Type
Type UDT2 Extends UDT1
Dim As Integer I2
End Type
Dim As UDT1 u1, u10 = (1)
Dim As UDT2 u2, u20 = (2, 3)
u1 = IIf(0, u10, u20)
Print u1.I1
u1 = IIf(1, u10, u20)
Print u1.I1
u2 = IIf(0 , u10, u20)
Print u2.I1; u2.I2
'u2 = Iif(1, u10, u20) ''Invalid assignment/conversion
SleepCategory: Miscellaneous Keywords
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing