Quote:
Originally Posted by MercifulBoss
Why would doing "days >= 366" advance forward a year? Wouldn't it simply say that if days = 366 or greater than 366 then do the below code?
|
You're correct. If days == 366 or days > 366, it would execute the code. That code includes advancing the year:
Code:
days -= 366;
year += 1;
Here's what would happen. On December 31, 2008, days == 366 and year == 2008, with the original code, it would enter the loop because days > 365, but never execute the leap year code because it expects days > 366. If you change that to days >=366, it will execute the code, which is subtract 366 from days, making it 0, and add 1 to year, making it 2009.
What you're left with is day 0, 2009. Besides the year being off, you also wind up with an invalid day, since day == 1 on January 1. Day 0 would be a day that doesn't exist. That code wouldn't cause an infinite loop, but it would think it was January 0, 2009. I'm not sure what kind of bugs that would introduce, but at the very least, the year will be 2009.
I might be wrong. As I said, my C is rusty, but I'm pretty sure that's what will happen.