FAQ: Booleans and Comparison Operators - Elseif Statements

This community-built FAQ covers the “Elseif Statements” exercise from the lesson “Booleans and Comparison Operators”.

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

Learn PHP

FAQs on the exercise Elseif Statements

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!

Could someone explain why this is not an acceptable answer to this exercise?

function whatRelation($percentage)
{
if ($percentage === 100) {
echo “identical twins”;
} elseif ($percentage < 100) {
echo “parent and child or full siblings”;
} elseif ($percentage < 35) {
echo “grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings”;
} elseif ($percentage < 14) {
echo “first cousins”;
} elseif ($percentage < 6) {
echo “second cousins”;
} elseif ($percentage < 3) {
echo “third cousins”;
} elseif ($percentage < 1) {
echo “not genetically related”;
}
}

Cheers!

Suppose the percentage of shared DNA is 12. According to the specifications, this should result in a statement of “first cousins”. But, this is not what will be printed if you use the code suggested by you.

The first if condition ($percentage === 100) will be FALSE, so the next condition will be checked.
The second condition ($percentage < 100) will be TRUE because indeed 12 is less than 100. So, the wrong message “parent and child or full siblings” will be printed.

You don’t need the elseif ($percentage < 100) condition. But even after deleting this particular condition, the other conditions also result in flawed logic. Instead consider using conditions involving the > or >= operator. That will help you in capturing the correct behavior.