Conditional Operators (Delphi)
Go Up to Delphi Compiler Directives (List) Index
If Conditional Operator
Syntax:
if <BoolExpr> then <thenValue> else <elseValue>
Remarks:
This conditional operator is similar in behavior to the ?: operator but the syntax is more Pascal-oriented, resembling the if-then-else construct.
The following example shows a simple assignment expression used in two versions; the first version is based on a traditional statement and the second on an if operator:
if Left < 100 then
X := 22
else
X := 45;
X := if Left < 100 then 22 else 45;
The key difference is that the operator can be used as part of any expression, as follows:
ShowMessage (if Left < 100 then 'Small' else 'Big')
Here you can see the change in grammar from the old expression to a newer one.
Old:
Expression -> SimpleExpression [ RelOp SimpleExpression ] . . .
New:
Expression -> SimpleExpression [ RelOp SimpleExpression ] . . .
-> IF Expression THEN Expression ELSE Expression
The type of the resulting expressions depends on the two types passed as parameters. Not all type combinations are legitimate. The table below shows a summary of the combinations:
| Type 1 | Type 2 | Result type |
|---|---|---|
| AnsiChar | AnsiChar | AnsiChar |
| AnsiString | AnsiString, AnsiChar | AnsiString |
| Array | Array | Array of the same element type. |
| Boolean types | Boolean types | Common Boolean type or Boolean type. |
| Class reference | Class reference, nil | Common type, Base type, or TClass. |
| Dynamic array | Dynamic array | Common dynamic array type. |
| Enum type | Enum type | Common enum type. |
| File | File | Error (use pointer to File type). |
| Instance | Instance, nil | Common type, Base type, or TObject. |
| Integral types | Integral types | Common integral type, or the type that covers both ranges. |
| Integral types | Real types | Real type. |
| Interface | Interface, nil | Common type, Base type, or IInterface. |
| nil | AnsiChar, ShortString, AnsiString, WideChar, WideString | Pointer to WideChar |
| Object | Object | Object type. |
| Pointer to AnsiChar | AnsiChar, ShortString, AnsiString literal | Pointer to AnsiChar |
| Pointer to AnsiChar | Pointer to WideChar literal | Pointer to WideChar |
| Pointer to WideChar | AnsiChar, ShortString, AnsiString, WideChar, WideString, UnicodeString | UnicodeString |
| Pointer types | Pointer types | Common pointer type, or Pointer type. |
| Procedure | Procedure | Procedure type, if they have the same signature. |
| Procedure of object | Procedure of object | Procedure for object type if they have the same signature. |
| Real types | Real types | The real type with the greater precision. |
| Record | Record | Record type. |
| Reference to procedure Anonymous Method |
Reference to procedure Anonymous Method |
Reference to the procedure type, if they have the same signature. |
| Set | Set | Common set type, or large set type if list literals. |
| Short string | Short string, AnsiChar | Longer short string |
| Text | Text | Error (use pointer to Text type). |
| UnicodeString | UnicodeString, WideString, AnsiString, Short string, WideChar, AnsiChar | UnicodeString |
| Variant | Variant | Variant type. |
| WideChar | WideChar | WideChar |
| WideChar | AnsiChar, AnsiString | UnicodeString |
| WideString | WideString, AnsiString, WideChar, AnsiChar | WideString |
The then or else expression can use a cast to guide the compiler's evaluation of the expression's type.
If the two types do not match and are incompatible, the compiler issues an error message as follows: [dcc32 Error] Unit31.pas(38): E2010 Incompatible types: 'string' and 'Integer'
It is important to note that the
if-then-else expression differs from the if-then-else statement, the expression has a single type. When the type of the then and else expressions differ, the expression's type is the common type of the two expressions' types; also known as the Least Upper Bound (LUB) type.When using the if-then-else operator within an expression, notice how other operations (e.g., addition) have higher priorities, which can lead to unexpected errors. Use extra parentheses, as is often done with low-priority Boolean operators.