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 trialGundra Kiran
Courses Plus Student 5,931 PointsI have a doubt in the quiz, Carefully, read the code below. Which customerAge ranges wonβt be handled correctly?
int customerAge;
float ticketPrice;
customerAge = 13;
if(customerAge < 13){
ticketPrice = 5.00
}
if (customerAge >= 65){
ticketPrice = 6.00
}
else{
ticketPrice = 10.00
}
I assumed ages from 13 to 64 wont be handled but answer is all ages under 13 how is it possible please explain?
1 Answer
Steve Hunter
57,712 PointsHi Gundra,
Let's say the customer age is 10. The first if
statement runs, making the ticketPrice
equal to 5.00. The code execution carries on - the customer is still agd 10. So, the next if
statement is entered. The customerAge
isn't over 65, so the else
clause runs setting ticketPrice
to 10.00.
There is nothing to halt execution after the first if
statement and the two if
statements are unrelated/unlinked. So, the first if
statement has no effect on the result of ticketPrice
as the second if
overrides it every time. Thus, the first test is the one that isn't handled correctly; under 13.
I hope that helps,
Steve.
Gundra Kiran
Courses Plus Student 5,931 PointsThe answer is clear and descriptive, thanks for the help.
Steve Hunter
57,712 PointsNo problem. Glad to help.
Steve.
Mazen Halawi
7,806 PointsMazen Halawi
7,806 Pointsticket price will be 10.0 since you mentioned its 10.00 in the else statement