while(...){ ... }else ...
Jens Bauer via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jan 18 07:52:23 PST 2015
I've sometimes wished for a while ... else combination in C.
-But I never got around to proposing my suggestion to whoever or
whatever committee is in charge of the C language.
But since D is a language that has much more freedom regarding
implementation, I'd like to suggest it here. :)
This is my usual code:
if(c >= '0' && c <= '9')
{
/* number */
while(c >= '0' && c <= '9')
{
c = *s++;
... do something with c ...
}
}
else if((c >= 'A' && c <= 'Z') || (c >='a' && c <= 'z') || c ==
'_')
{
/* token */
while((c >= 'A' && c <= 'Z') || (c >='a' && c <= 'z') || c ==
'_')
{
c = *s++;
}
}
else if(debug)
{
/* error */
}
... The arguments for 'if' and 'while' are the same, and the
initial test could be reduced to a single test instead of
multiple tests...
while(c >= '0' && c <= '9')
{
/* number */
c = *s++;
... do something with c ...
}
else while((c >= 'A' && c <= 'Z') || (c >='a' && c <= 'z') || c
== '_')
{
/* token */
c = *s++;
}
else if(debug)
{
/* error */
}
... Ah, much more readable.
This makes it possible to repeat actions, in case a statement is
true, generalizing the while statement slightly.
Thus... If we for instance enter the first while(...), then we
will not enter any of the 'else' statements.
More information about the Digitalmars-d
mailing list