Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Delphi=== In [[Object Pascal|Delphi]] the {{code|IfThen}} function can be used to achieve the same as {{code|?:}}. If the {{code|System.Math}} library is used, the {{code|IfThen}} function returns a numeric value such as an [[Integer (computer science)|Integer]], [[Double precision|Double]] or Extended. If the {{code|System.StrUtils}} library is used, this function can also return a [[string (computer science)|string]] value. Using {{code|System.Math}} <syntaxhighlight lang="Delphi"> function IfThen(AValue: Boolean; const ATrue: Integer; const AFalse: Integer): Integer; function IfThen(AValue: Boolean; const ATrue: Int64; const AFalse: Int64): Int64; function IfThen(AValue: Boolean; const ATrue: UInt64; const AFalse: UInt64): UInt64; function IfThen(AValue: Boolean; const ATrue: Single; const AFalse: Single): Single; function IfThen(AValue: Boolean; const ATrue: Double; const AFalse: Double): Double; function IfThen(AValue: Boolean; const ATrue: Extended; const AFalse: Extended): Extended; </syntaxhighlight> Using the {{code|System.StrUtils}} library <syntaxhighlight lang="Delphi"> function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ''): string; </syntaxhighlight> Usage example: <syntaxhighlight lang="Delphi"> function GetOpeningTime(Weekday: Integer): Integer; begin { This function will return the opening time for the given weekday: 12 for Sundays, 9 for other days } Result := IfThen((Weekday = 1) or (Weekday = 7), 12, 9); end; </syntaxhighlight> Unlike a true ternary operator however, both of the results are evaluated prior to performing the comparison. For example, if one of the results is a call to a function which inserts a row into a database table, that function will be called whether or not the condition to return that specific result is met.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)