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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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

1 Like

If i assign to $dna_percentage a value of over 100 it prints a response: “parent and child or full siblings” (as it should according to the statements).I added an extra statement that prints “Not applicable” but i can see that it works only if a negative number is assigned: whatRelation(-12) → prints "Not applicable.
The question is how can i limit the value of $dna_percentage to maximum 100 so that it will also print "Not applicable when a value higher than 100 is printed.This is my code:

 <?php
namespace Codecademy;

 function whatRelation($dna_percentage)
 {
   if($dna_percentage === 100)
   {
     echo "identical twins";
   }elseif($dna_percentage > 34 ){
     echo "parent and child or full siblings";
   }elseif($dna_percentage > 13){
     echo "grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings";
   }elseif($dna_percentage >5){
     echo "first cousins";
   }elseif($dna_percentage > 2){
     echo "second cousins";
   }elseif($dna_percentage >0){
     echo "third cousins";
   }elseif($dna_percentage === 0){
     echo "not genetically related";
   }else {
     echo "Not applicable";
   }

   
 }

whatRelation(100);
echo "\n";
whatRelation(56);
echo "\n";
whatRelation(3);
echo "\n";
whatRelation(667);

I found a way to do it.If i add this statement right after the “if” statement it can print a different value than “Not applicable” (“Value too high”).It is not exactly the same as i wanted so any help would still be appreciated.

elseif($dna_percentage > 100){
     echo "Value too high";
   }


Which integer is les than 3 but bigger then 2 ? :smiley:

Yes, the feedback isn’t quite correct because the instructions state “We expect the number passed in to always be an integer from 0 to 100”.
However, I would suggest that other than the first condition (the one with $grade > 99), for all the other conditions, you should use greater than or equal to i.e. you should use >= instead of >
For example the condition ($grade > 35) should be edited to ($grade >= 35) because the specifications state: 35 through 99 should print "parent and child or full siblings"

They probably tested your program with an input of 3. According the specs: 3 through 5 should print "second cousins". But since you used ($grade > 3), so the third cousins condition was invoked instead.

Hi !

Here, my code.

<?php
namespace Codecademy;

function whatRelation($percent): string
{
  if ($percent == 100)
  {
    return "identical twins";
  } else if ($percent >= 35) 
  {
    return "parent and child or full siblings";
  } else if ($percent >= 14)
  {
    return "grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings";
  } else if ($percent >= 6) 
  {
    return "first cousins";
  } else if ($percent >= 3) 
  {
    return "second cousins";
  } else if ($percent >= 1)
  {
    return "third cousins";
  } else {
    return "not genetically related";
  }
}

And I have tested all conditions.

echo whatRelation(100);
echo "\n";
echo whatRelation(99);
echo "\n";
echo whatRelation(14);
echo "\n";
echo whatRelation(13);
echo "\n";
echo whatRelation(3);
echo "\n";
echo whatRelation(2);
echo "\n";
echo whatRelation(0);

And give that on output:

identical twins
parent and child or full siblings
grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings
first cousins
second cousins
third cousins
not genetically related

I received n error message like:

Your function should print the string "identical twins" if invoked with 100. We tested your function with 100 and expected "identical twins" but instead it printed ""

I don’t explain what is it going on with the site unit test…

The instructions specify:

Write a function, whatRelation() that has one number parameter representing the percentage of DNA the two people share. Your function should print the likely relationship as a string.

In your code, you are returning strings and then printing them outside the function

function whatRelation($percent): string
{
  if ($percent == 100) {
    return "identical twins";
  } else if (...
}

echo whatRelation(100);

According to the instructions, the function is supposed to print (not return) the result.

function whatRelation($percent)
{
  if ($percent == 100) {
    echo "identical twins";
  } elseif (...
}

whatRelation(100);
1 Like

i used Logical Operator with that exercise, using if else statement.
i know it is more elaborated but it is not wrong i guess

function whatRelation($number){
if($number===100){
return "identical twins";
}
elseif($number>=35 && $number<100){
return "parent and child or full siblings";
}
elseif($number<=34 && $number>=14){
return "grandparent and grandchild, aunt/uncle and niece/nephew, or half siblings";
}
elseif($number<=13 && $number>=6){
return "first cousins";
}
elseif($number<=5 && $number>=3){
return "second cousins";
}
elseif($number==2 && $number==1){
return  "third cousins";
}
else
return  "not genetically related";
}

echo whatRelation(87);