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 trial

iOS Objective-C Basics Introduction to Operators and Conditionals Review IF ELSE

Gundra Kiran
PLUS
Gundra Kiran
Courses Plus Student 5,931 Points

I 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?

Mazen Halawi
Mazen Halawi
7,806 Points

ticket price will be 10.0 since you mentioned its 10.00 in the else statement

1 Answer

Hi 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
Gundra Kiran
Courses Plus Student 5,931 Points

The answer is clear and descriptive, thanks for the help.

No problem. Glad to help. :smile:

Steve.