Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This proposed rewrite is provided by "A lesson on infinite loops". The loop is guaranteed to exit, as days decreases for each iteration of the loop, unless the while condition fails, and in which case the loop terminates.

Code Block
bgColor#ccccff
final static int ORIGIN_YEAR = 1980;
UINT32 days = /* number of days since January 1, 1980 */
int year = ORIGINYEAR;
/* ... */

int daysThisYear = (IsLeapYear(year) ? 366 : 365);
while (days > daysThisYear) {
  days -= daysThisYear;
  year += 1;
  daysThisYear = (IsLeapYear(year) ? 366 : 365);
}

...