Killing the comma operator

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Wed May 11 09:46:48 PDT 2016


On Wednesday, 11 May 2016 at 13:29:56 UTC, Gopan wrote:
> int x;
> while( scanf("%d", &x),  x!= 0) // until user input 0.
> {
>    //do something with x
> }
>
> Without the comma operator, I would have to repeat the scanf 
> statement.
> int x;
> scanf("%d", &x);
> while(x != 0)
> {
>    //do something with x
>    scanf("%d", &x);
> }

Aside from scanf specifics, you shouldn't repeat the setup code, 
use do...while(true) with if and break.

In places where the comma operator does help,  use a 
comma(expr,result) template function, implemented here:
http://forum.dlang.org/post/ngslcl$otg$1@digitalmars.com


More information about the Digitalmars-d mailing list