Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKat Stacy
41,658 PointsPing Pong Logical Operators
//continue the game while the absolute value is less than ;
while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11)
Why do you use the logical operators where you do. In Alena's game, the score starts out with an absolute value of 5. So wouldn't the game be over if you use the || or logical operator where you do? If the absolute value needs to be greater than 2 for the loop to stop or the players must have a score of greater than 11 to win then I think the game should have ended before it began. Shouldn't the first logical operator be &&? And why is the second logical operator && shouldn't it be || since we only want one of the players to have a score greater than 11 to win? I am so confused by this lesson.
2 Answers
Niki Molnar
25,698 PointsI'm not sure which of the ping-pong lessons you are referring to but the correct IF statement should be:
if( abs( $player1 - $player2 ) >= 2 && ( $player1 >= 11 || $player2 >= 11 ) ) {
// Someone won
} else {
// Game continues
}
So yes, you're right about the middle && operator
Kat Stacy
41,658 PointsI think I know the answer to my question of why the certain logical operators where used.
while (abs($player1 - $player2) < 2 || $player1 < 11 && $player2 < 11)
While either of the conditions are true then the loop will continue to run. Even thought the first condition is false (bc the abs starting value is more than 2) the second condition is true so the while loop will continue to run untill both conditions are false.
Kat Stacy
41,658 PointsKat Stacy
41,658 PointsI am referring to the lesson in the Beginner PHP track, in the PHP Arrays and Control Structures Course, in the PHP Loops section, the video lesson titled Ping Pong. Alena starts off by stating the conditions that need to be present in order for there to be a winner for a "ping-pong game". She says that for a player to win the game at least one player must have a score of 11 and that the winner must have at least 2 points higher than the loser. She said that both of these things need to be true for there to be a winner. But in the "while" statement that she writes for the game she uses the || to imply that one or the other needs to be true. And she uses a && to imply that both people need to have a score higher or equal to 11. I would send you a picture of this code but I am sorry I do not know how.
This is what she writes: