If Statement with Declaration

Jerry via Digitalmars-d digitalmars-d at puremagic.com
Fri Nov 4 23:45:29 PDT 2016


On Friday, 4 November 2016 at 18:05:51 UTC, Adrian Matoga wrote:
> On Friday, 4 November 2016 at 15:34:00 UTC, Jerry wrote:
>> On Friday, 4 November 2016 at 14:16:44 UTC, Matthias Bentrup 
>> wrote:
>>> On Thursday, 3 November 2016 at 22:29:34 UTC, Jerry wrote:
>>>>     if(int i = someFunc(); i >= 0)
>>>>     {
>>>>         // use i
>>>>     }
>>>> Thoughts on this sort of feature?
>>>
>>> I would prefer the syntax
>>>
>>>   if( (int i = someFunc()) >= 0 )
>>>   {
>>>     // use i
>>>   }
>>>
>>> as this matches the already existing assignment expression 
>>> syntax if you pull the declaration out of the if expression.
>>
>> Well you can argue the "if( init ; condition )" syntax matches 
>> "for( init ; condition ; update)", minus the "update" section.
>
> You've just answered your own question:
>     for (int i = someFunc(); i >= 0; )
>     {
>         // use i
>         break;
>     }

Yes, it was one of the examples I gave in the first post. Reason 
not to use that is, if you forget "break" you are probably going 
to have an infinite loop. As well you can't use "else" with a for 
statement.



More information about the Digitalmars-d mailing list