Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 23, 2007 Expression evaluation order in if statement | ||||
---|---|---|---|---|
| ||||
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 ... } } or should I rather use: void func(char[] str) { if (str !is null) if (str=="somestring") { ... some code ... } } The second way is more explicit and makes code unreadable soon... I can find clear answer in docs... BR Marcin Kuszczak (Aarti_pl) |
August 23, 2007 Re: Expression evaluation order in if statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | 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." |
August 23, 2007 Re: Expression evaluation order in if statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lutger | 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!
BR
Aarti_pl
|
August 23, 2007 Re: Expression evaluation order in if statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | 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)
|
August 23, 2007 Re: Expression evaluation order in if statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frits van Bommel | Frits van Bommel wrote:
> 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)
If you're referring to me, I wasn't complaining about this behaviour at all, this behaviour is great, I hope it never changes.
Regan
|
Copyright © 1999-2021 by the D Language Foundation