While looking through some example C code, I came across this:
y -= m < 3;
What does this do? It it some kind of condensed for loop or som
I means if (m < 3) { y -=1; }
, since (m < 3)
is 1 if m is less than 3, 0 otherwise.
The code appears in some hoary old reference implementation of something to do with either leap years or Easter, or possibly both: the first two months January and February are special because they occur before the leap day. There isn't really any excuse for writing code like that, unless you actually like the look of it. Most people don't.
If you break it down by order of precedence for each operation, you get:
y = (y - (m < 3));
m < 3
gets evaluated and returns a boolean result 1
or 0
, so the expression can be simplified as
y = y - 1; // if m < 3 is true
or
y = y - 0; // if m < 3 is false
The purpose for doing this is to avoid an if
clause.
I can't tell you want it's for, but I can tell you what it does:
m < 3
returns an int of 0 or 1 representing a boolean value.
if m is less than 3, the statement evalutates as: y -= 1
or y = y - 1
;
if m is greater than or equal to 3, the statement evalutates as y -= 0
or y = y - 0
. Overall in this case, the statement does nothing.
m < 3
evaluates to 1 if m is less than 3. Hence, y is decreased by one in this case. Thus, an if statement avoided.
I agree that the code is equivalent to if(m<3) y=y-1;
However it is unsafe code because while FALSE is always 0, TRUE has had a couple of definitions. Nowadays TRUE is equal to 1 in most programming systems, but in older systems it was equal to -1 (where all bits where set to on, not just the last one).
So if this code is running on an older platform it could translate to if(m<3) y=y-(-1);
or if(m<3) y=y+1;
Cross system compatibility is a big reason to avoid 'clever' code like this.
m < 3
is either 1
or 0
, depending on the truth value.
So y=y-1
when m<3
is true
, or y=y-0
when m>=3