M
mathog
C has two () constructs which return a value:
( expr1 ? expr2 : expr3)
( expr )
As far as I can tell the language does not define a two expression
variant. Is there some way to concoct a two expression variant that
acts like this (whatever the solution is, it has to go within () ):
( expr1 ?? expr2 ) // ?? isn't legal. used to show approx. syntax
Where
the value returned is expr1
expr2 is always evaluated
expr2 is evaluated after expr1
The value of expr2 is not used for the () logic, it is just
evaluated for side effects.
Or described another way:
in any if() or while(), within the parens
val=expression1
do some other stuff (at least one expression, more would be better)
the test is applied to val
Here is a simple example of its use with the approx. syntax in a while loop:
while( *ptr ?? {ptr+=1;val--;} ){
Notice that at the moment there seems to be no place to put the val--,
other than with a lot of extra logic, and the +=1 is only possible
because of the way ++ works:
didval=0;
while(*ptr++){
val--;
/* code */
if(whatever){didval=1; break;}
/* more code */
}
if(!didval)val--;
If we give up on putting everything within the while parens the logic is
at least easier to understand:
while(1){
tval=*ptr;
ptr+=1;
val--;
if(!tval)break;
/* code */
}
This can be accomplished by calling a function within the parens.
Can it be done without calling a function and with all implementing code
within the parens???
Thanks,
David Mathog
( expr1 ? expr2 : expr3)
( expr )
As far as I can tell the language does not define a two expression
variant. Is there some way to concoct a two expression variant that
acts like this (whatever the solution is, it has to go within () ):
( expr1 ?? expr2 ) // ?? isn't legal. used to show approx. syntax
Where
the value returned is expr1
expr2 is always evaluated
expr2 is evaluated after expr1
The value of expr2 is not used for the () logic, it is just
evaluated for side effects.
Or described another way:
in any if() or while(), within the parens
val=expression1
do some other stuff (at least one expression, more would be better)
the test is applied to val
Here is a simple example of its use with the approx. syntax in a while loop:
while( *ptr ?? {ptr+=1;val--;} ){
Notice that at the moment there seems to be no place to put the val--,
other than with a lot of extra logic, and the +=1 is only possible
because of the way ++ works:
didval=0;
while(*ptr++){
val--;
/* code */
if(whatever){didval=1; break;}
/* more code */
}
if(!didval)val--;
If we give up on putting everything within the while parens the logic is
at least easier to understand:
while(1){
tval=*ptr;
ptr+=1;
val--;
if(!tval)break;
/* code */
}
This can be accomplished by calling a function within the parens.
Can it be done without calling a function and with all implementing code
within the parens???
Thanks,
David Mathog