0

I'm doing a practice exercise. It asks me to create a calendar of the month, year which is the current user time. I have looked up some code on the Internet, it works well, but I can't understand it clearly. Especially the line year -= month < 3. Can someone explain it, please?

//return the daycode of the first day of month.
int firstDayOfMonth(int month, int year) {
    int dow = 0; 
    int day = 1;

    int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    year -= month  < 3; // I cannot understand this.
    cout<<"This is year "<<year<<endl;
    dow = ( year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;

    return dow;
}

int main()
{
    int a;
    cout<<firstDayOfMonth(2,2018)<<endl;
    return 0;
}
Kon
  • 4,023
  • 4
  • 24
  • 38
  • You could try printing out the result of just that line. The result is true or false right? What do those represent in integer values? – Retired Ninja Oct 23 '18 at 01:11
  • 1
    "It asks me to" how about writing it yourself instead of "downloaded these code"? – Swordfish Oct 23 '18 at 01:14
  • You're not *doing a practice exercise* if you're copy/pasting someone else's code to complete it. *Doing a practice exercise* is writing the code to do it yourself, so that you actually learn something other than how to copy/paste. – Ken White Oct 23 '18 at 01:18
  • For what it's worth (and as ruakh points out in his answer): The bit of code you're asking about isn't written for readability. This kind of way of writing code can throw off even experienced programmers, because it's making use of a "trick" of the language. This is frowned upon especially in production code bases because many people may have to read the code, and understand it, and that takes time (which costs money), and can frustrate programmers. Unless there is a good need (such as a speed optimization for critical code), write *clear readable code*. – Steve Oct 23 '18 at 01:50
  • I say it makes use of a "trick" of the language because of C, and my (personal) desire to represent things as they are (`bool` can be `true` or `false`, it's not even a number to me). – Steve Oct 23 '18 at 01:51

2 Answers2

4

In C++, boolean values can be implicitly converted to integers, with false becoming 0 and true becoming 1. (See bool to int conversion.)

So year -= month < 3; is equivalent to:

if (month < 3) {
    year -= 1; // true -> 1
} else {
    year -= 0; // false -> 0
}

which can be simplified to:

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

The motivation is that January and February (months 1 and 2) come before any leap day, while the other months come after any leap day, so it's convenient to treat January and February as being at the end of the previous year, and let the leap day be added to the calculation for the entire March-to-February year.

This code is obviously not optimized for readability.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • +2 for "This code is obviously not optimized for readability." -1 for not making that a bigger deal. – Steve Oct 23 '18 at 01:46
1

What that means is:

if the condition (month < 3) is true, then decrement by 1. if the condition (month < 3) is false, then decrement by 0 (year stays the same)

The value of 1 & 0 represent false & true of the month & number comparison.

THEOS
  • 59
  • 5