Expression evaluation order in if statement

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Aug 23 04:30:12 PDT 2007


Aarti_pl wrote:
> Lutger pisze:
>> Aarti_pl wrote:
>>> Hello!
>>>
>>> Can I use if expression in following way, or is it an error?:
>>>
>>> void func(char[] str) {
>>>     if (str !is null && str=="somestring") {
>>>         ... some code ...
>>>     }
>>> }
>>
>> It's ok, see: 
>> http://www.digitalmars.com/d/expression.html#AndAndExpression
>>
>> "If the left operand, converted to type bool, evaluates to false, then 
>> the right operand is not evaluated."
> 
> Great.
> Thanks for advice!

Also, for arrays "null" is just an empty array, so you don't need the 
first part: 'if (str == "something")' works just fine even if 'str' is 
null (unlike with classes).
(A while back some people were complaining about this, but I don't think 
this behavior is likely to change)


More information about the Digitalmars-d-learn mailing list