Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.