Skip to content

Intentional Delphi

Adventures in the act of coding

Menu
  • Blog
  • Books
  • About
Menu

The Conditional (Ternary) Operator

Posted on October 20, 2025October 20, 2025 by admin

Many have asked, over the years, why Delphi does not include a ternary operator. Now Delphi 13 Florence adds the conditional operator to the language, which is that ternary operator.

The syntax is simple:
if <cond> then <thenValue> else <elseValue>

In use, you may see:
X := if Left < 100 then 22 else 45;
or
ShowMessage(if Failed then 'Failed' else 'Success');

The conditional operator is an expression, so it is evaluated, and the value is returned.

This is a welcome addition to the language, and when used well, it will increase clarity in your code, generally where you will be condensing inline processing. In cases where the condition itself is complex, or where the values to be returned are not simple and small, then the use of explaining variables will be useful. Or you may simply choose to employ the more conventional coding, without the conditional operator.

Some years ago, the IfThen() functions were added to the Delphi libraries. These gave some convenience, but were just functions with specific areas of support. The new conditional operator is altogether different, as may be seen in the help page which presents a substantial chart of the various types and how they are handled by the conditional operator. There is much content on that page, and I recommend you give it your attention.

Although the IfThen() functions were convenient, the conditional operator is a first-class member of the language, as this table of types clearly shows, and adds real value to the language.

Type 1Type 2Result type
AnsiCharAnsiCharAnsiChar
AnsiStringAnsiString, AnsiCharAnsiString
ArrayArrayArray of the same element type.
Boolean typesBoolean typesCommon Boolean type or Boolean type.
Class referenceClass reference, nilCommon type, Base type, or TClass.
Dynamic arrayDynamic arrayCommon dynamic array type.
Enum typeEnum typeCommon enum type.
FileFileError (use pointer to File type).
InstanceInstance, nilCommon type, Base type, or TObject.
Integral typesIntegral typesCommon integral type, or the type that covers both ranges.
Integral typesReal typesReal type.
InterfaceInterface, nilCommon type, Base type, or IInterface.
nilAnsiChar, ShortString, AnsiString, WideChar, WideStringPointer to WideChar
ObjectObjectObject type.
Pointer to AnsiCharAnsiChar, ShortString, AnsiString literalPointer to AnsiChar
Pointer to AnsiCharPointer to WideChar literalPointer to WideChar
Pointer to WideCharAnsiChar, ShortString, AnsiString, WideChar, WideString, UnicodeStringUnicodeString
Pointer typesPointer typesCommon pointer type, or Pointer type.
ProcedureProcedureProcedure type, if they have the same signature.
Procedure of objectProcedure of objectProcedure for object type if they have the same signature.
Real typesReal typesThe real type with the greater precision.
RecordRecordRecord type.
Reference to procedure Anonymous MethodReference to procedure Anonymous MethodReference to the procedure type, if they have the same signature.
SetSetCommon set type, or large set type if list literals.
Short stringShort string, AnsiCharLonger short string
TextTextError (use pointer to Text type).
UnicodeStringUnicodeString, WideString, AnsiString, Short string, WideChar, AnsiCharUnicodeString
VariantVariantVariant type.
WideCharWideCharWideChar
WideCharAnsiChar, AnsiStringUnicodeString
WideStringWideString, AnsiString, WideChar, AnsiCharWideString

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Delphi Legacy Projects
  • New! PDF available below
  • Delphi Legacy Projects (PDF)
  • Upgrading and Maintaining Delphi Legacy Projects
MVP Directory

© 2025 Intentional Delphi | Powered by Minimalist Blog WordPress Theme