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:

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 () 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.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? 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

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.

1 Like

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

3 Likes

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)
or
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++:

https://en.cppreference.com/w/cpp/language/operator_precedence

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?

1 Like

hey guys
please i need assistance with the hint, why did they write
int y = 0
like is it necessary can`t i just write int y ?
and also it brings me to this part with is kind of confusing, like mathematically i don´t think it´s right

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

It’s a good habit and in some languages its required to declare an initialize variables. You could but you might get unexpected behavior/results.

Order of operation still exist as well.

if Y divided into 4 has no remainder and y divided into 100 does not a remainder OR y divided into 400 has no remainder, is what it is saying. Whether that is expected results or not is another thing.

i think the instructions are unclear:

• When you say “can be evenly divided by 100”, doesn’t that mean it should be exact? there should be no remainders, but on the answer it says “y % 100 != 0”

You have taken the instructions out of context:

• 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.

the however on the previous step is very crucial. In fact, these two steps have to be read together.

I found this website helpful in solving the leap-year exercise as the instructions were explained in more detail and steps through the process.
Thank you,

https://www.tutorialspoint.com/cplusplus-program-to-check-leap-year

1 Like

So for like 3 or 4 programs the bash will give me errors about something thats literally correct…i’ve even copyed the hint program and it is the same , what should i do ?? it is the problem from me or from the site??

1 Like

same hAPPENED WITH ME

1 Like

Hey I think i got it …soo when you make some changes in your code make sure you hit the RUN button , and then compile , that works for me !

1 Like

hey guys, I happen to study functions and forgot about this exercise. So this is my Try, I wanted a function to count the number of digits the user had entered and if that digit is equal to 4 check if the year is a leap_year or no. about the instructions, they’re not clear so I found those instead: Return true if year is a multiple 0f 4 AND not multiple of 100 OR year is multiple of 400. I recommend to complete C++ Functions before going back and look at this code.This is an additional for y’all.

Sir, program is not working, compiling doesn’t show any error and it gets compiled properly but the program is not running it shows nothing? Just as it is? But it is running in code blocks?

Hello @data4005071764 and welcome to the Codecademy Forums!

Please explain exactly what issue you are having and post any relevant code.

they should include your explanation in the tutorial, this helped me understand it. Thank you!

The wording of the “however” was not precise or specific enough to me which is why I thought it meant 100==0 || 400==0. Your link shows that if “year is divisible by 100 then it is not a leap year”; the wording made things far more confusing, so thanks for clearing that up. Of course if you know how leap years work none of this is necessary.

I think a majority of people doesn’t know that 1700, 1800 and 1900 are not leap years.

Part of programming is also being able to study requirements thoroughly, so you know what you are going to implement. Its not only about some code

it took me a while but i completed the code satisfactory and when i ran the program it asked me the question and when i input the year it would calculate and spit out the correct answer… the issue is that the website itself wont let me move forward from this point. i even went so far as to delete my code (which i was unhappy about because i felt an accomplishment for coming up with it) and putting in the “answer” on the “get unstuck” section. after doing this, the program runs the exact way i had it originally and the website still will not let me move forward… can someone help?