Sim*_*mon 25 delphi operators ternary-operator
可能重复:
Delphi中是否存在或者是否存在条件运算符?
我知道Delphi没有C#中的三元运算符.即?:
那么如何最好地表示这个函数调用?什么是最干净的方法?
如果有任何代码可以使用INSTEAD编写单独的函数,那将是非常好的吗?如果没有,那么最有效和/或最干净的代码表示是什么?
And*_*and 41
当然可以使用
IfThen(SomeBooleanExpression, IfTrueReturnValue, IfFalseReturnValue)
Run Code Online (Sandbox Code Playgroud)
其中返回值为numeric(uses Math
)或string(uses StrUtils
).但请注意,这将在所有情况下评估两个参数 - 没有惰性求值,因此它不如?:
C#中的运算符有效,其中只评估正确的操作数.
所以你做不到
y := IfThen(x <> 0, 1/x, 0)
Run Code Online (Sandbox Code Playgroud)
最好的事情就是坚持平凡
if x <> 0 then y := 1/x else y := 0;
Run Code Online (Sandbox Code Playgroud)
最接近三元运算符的是:
if (condition) then <statement> else <statement>;
Run Code Online (Sandbox Code Playgroud)
据我所知,Delphi中没有三元运算符。