Leap year exercise

// y = year

else if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {

``````std::cout << y;
std::cout << ", Lucky you, this is a leap year\n";
``````

so as a whole this challenge wasn’t too crazy for me and I get the rest of the code.
the part I’m not understanding is why this, else if, is stated this way

the way I read it is if y is divided into 4 with no remainder AND y is divided into 100 with a remainder not equal to 0 or y is divided into 400 evenly with no remainder, this is a leap year.

the problem if the != portion. It means not equal to, right?
I feel it"s one of those things that I understand but my brain can"t fully grasp the concept of how it"s working code wise

Yes, you are correct. `!=` means not equal.

1 Like

Thanks! so im assuming, its saying either 4 is divided evenly and 100 is not or 4 is divided evenly and 400 is as well. when either of those pairs is both true, that makes it a leap year. Is that a good way to word it?

1 Like

Actually, the way that would work is if 4 goes evenly into the year AND 100 does NOT go evenly into the year it’s a leap year OR if 400 goes evenly into the year it’s a leap year.

1 Like

Man, makes perfect sense now. Thank you so much for taking the time to help over something so trivial… massive help.

1 Like

You’re welcome! Happy to help.

I still don’t understand this. If 100 does not evenly go into the year, wouldn’t it not be a leap year?

If 4 does AND 100 does not, it’s a leap year. If 400 does, it’s a leap year.

Quote:

The Gregorian Calendar has become the internationally accepted civil calendar. The leap year rule for the Gregorian Calendar differs slightly from one for the Julian Calendar. The Gregorian leap year rule is: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 are not leap years, but the year 2000 is. The Gregorian dates for Easter are computed from a set of ecclesiastical rules and tables.

So does it go like this in terms of order?

if divisible by 4 = true and divisible by 100 does not equal 0 then statement is true and program moves on to display this is a leap year?
if divisible by 100 does = 0 than statement is false and will bring the OR into concern?

It does go from left to right. If the year is exactly divisible by 4, the next expression is evaluated, so we check if the year is not exactly divisible by 100. If that is true, then both expressions on either side of the `&&` are true, so the `if()` condition is satisfied at that point, and the code inside the `if` block executes. If either of those expressions evaluates to false then the condition to the right of the `||` operator is checked. If the year is exactly divisible by 400 the `if()` condition is satisfied, and the code executes. The condition following `||` (or) is only evaluated if either of the conditions preceding it is false.

3 Likes