Skip to main content

IfThen

Function IfThen(AValue: Boolean; const ATrue: string; AFalse: string): string

Example

procedure ScriptEvent(var Value: variant);
var
IsPriority: Boolean;
begin
IsPriority := True;
Value := IfThen(IsPriority, 'Priority', 'Standard');
// Priority
end;

Usage

IfThen returns one of two already-evaluated strings according to a Boolean condition.

Parameters

NameTypeDescription
AValueBooleanExplicit condition controlling which string is returned.
ATruestring, constResult when AValue is true.
AFalsestringResult when AValue is false. Required by the Velox script declaration.

Supply all three arguments in scripts.

Returns

Exactly ATrue or AFalse; the selected string is not trimmed, formatted or converted further by the function.

Errors

Argument evaluation, Boolean/string conversion and allocation errors propagate. The unselected expression can raise and prevent the call.

Usage notes

Prefer an if ... then ... else statement for control flow and IfThen for concise selection between safe, already computed strings. Always pass AFalse in Velox scripts.

Additional Technical Info

IfThen returns ATrue when AValue is true and AFalse otherwise. Velox exposes the string overload from installed Delphi System.StrUtils; it is a function call, not the Pascal if statement.

Both string arguments are evaluated before the function chooses one. The example is fictional, deterministic and source-reviewed and was not executed by the documentation workflow.

Implementation

uPSI_StrUtils registers the compile declaration and runtime pointer to System.StrUtils.IfThen. Installed Studio 37.0 source performs one Boolean branch and one string assignment:

if AValue then
Result := ATrue
else
Result := AFalse;

Velox adds no wrapper, validation, logging or exception handling.

Eager evaluation is the central constraint

Function arguments are evaluated before entry. This differs from a language if statement:

// Both BuildPriorityText and BuildStandardText run before IfThen chooses.
Value := IfThen(IsPriority, BuildPriorityText(), BuildStandardText());

Use a real if ... then ... else statement when a branch is costly, changes state, can raise, reads a dataset cursor or must not execute. IfThen is appropriate for already available strings and simple side-effect-free expressions.

Condition and type behavior

  • AValue is statically Boolean. Numeric/text values must satisfy PascalScript's argument conversion before the function starts; IfThen itself does not interpret truthy/falsy business values.
  • Both possible results are statically strings. Variant Null, records and objects are not accepted as branch values without a prior conversion.
  • Empty strings, whitespace and identical branch strings are all valid.
  • The function reports no branch indicator besides the returned value.
  • It selects strings only; other native IfThen overloads are not registered under this Code Library entry.

Side effects

None in the installed function. Side effects in AValue, ATrue or AFalse expressions happen before entry, including effects in the branch not selected.

Performance and concurrency

One Boolean branch and normal reference-counted string assignment after argument evaluation. No shared mutable state, locale dependency or locking.

Related entries

  • IfBlank selects a fallback using exact empty-string state.
  • IfNull selects between Variants using Null equality.
  • CoalesceString selects between strings using exact empty first text.

External references

Created 2026-07-15