Search code examples
c++syntaxcomparisonoperators

Cannot understand uncommon syntax


I have recently come across a function which calculates the day of the week for any given date. The function is shown below.

unsigned int getDayOfWeek(const unsigned int day, const unsigned int month, unsigned int year)
{
    static unsigned int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    year -= month < 3;
    return ( year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}

I am having trouble understanding the syntax of year -= month < 3. I am assuming it expands to year = year - (month < 3), however I still cannot understand what this does.

My question is: what does this syntax do in general, not just in the context of this function? For example a -= b < 3.

Thank you in advance.


Solution

  • month < 3 is a boolean expression.

    • false converts to 0
    • true converts to 1.

    You might rewrite it as:

    if (month < 3) { year = year - 1; }