FAQ: Logical Operators - Review

This community-built FAQ covers the “Review” exercise from the lesson “Logical Operators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m having trouble understanding the logic of the instructions and the hint for the review.

The instructions are:

  • If the year can be evenly divided by 4 then it is a leap year, however…
  • If that year can be evenly divided by 100, and it is not evenly divided by 400, then it is NOT a leap year.
  • If that year is evenly divisible by 400, then it is a leap year.

The hint says:

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

Surely it should be as according to instructions:

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

Maybe the y % 100 != 0 is a typo?

Also how is any leap year evenly divisible by 400??? For example 2004 was a leap year. 2004 % 400 = 4 which is not evenly divisible.

The hint is correct.

but if i look at the hints:

  • If the year can be evenly divided by 4 then it is a leap year, however…
  • If that year can be evenly divided by 100, and it is not evenly divided by 400, then it is NOT a leap year.

you should really read them together, the however on the previous line is crucial there.

lets explain this one first from a human perspective, then we will get back to the code in a second. A little know fact about leap years, is that for centuries (1500, 1600, 1700, 1800, 1900, 2000) and so forth, there is a special rule. A century is only a leap year when divisible by 400. So 1600 and 2000 are leap years, but 1700, 1800 and 1900 are not

Then its good to realize, that and (&&) operator is evaluated before the or (||) operator.

So the best way for to express this in code, is to check if a year is divisible by 4 but not divisible by 100. This way, we deal with 1700, 1800 and 1900 which are not leap years.

so for 2004 we get:

2004 % 4 == 0 && 2004 % 100 != 0

we get:

0 == 0 && 4 != 0

so we are good.

lets also do 1900:

1900 % 4 == 0 && 1900 % 100 != 0

results in:

0 == 0 && 0 != 0

then we get:

true && false

which is false, which is good. 1900 is not a leap year

then finally we have % 400 to deal with 1600, 2000 and so forth

hope this helps

1 Like

I have a question…

…you know how there are orders of operation in regular math? (I mean Bedmas) This order can change the answer to the expression: 5 + 5 / 5 there are two scenarios;
a) ignoring Bedmas --> 2
b) Bedmas --> 6
Of course in proper mathematics, the answer is always going to be b)
My question is,
a) are there similar rules in coding with the and and or conditions
b) Can I use brackets?

For example, if (200 > 300 && 200 > 100 || 200 > 99)
of, further simplified if (false && true || true)

Would this expression evaluate to true and have the code run, or would it evaluate to false?

Are we looking at it like this:
if (false && true)
if (false || true)

As for my second question,
as in Bedmas, could I use brackets?
for example, could I do something like
if (false && (true || true))
(which would evaluate to false)

Please reply, I feel this information would be very important, of course I could just experemint on my own, but I hope someone will reply

Thankfully programming also has a fixed order of operations, otherwise things would become a mess. Here is the list for c++:


a) yep, they are in the list i just posted

b) yep, you can use brackets

if brackets where not used/possible, false && true || true would be true. So you answered your own question?