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

If / Else statement with Bool

Hello,

I don't get this part, actually I'm confused with they tell me to do. Unless I didn't wrote it properly, isn't "hasBonus = true" correct ? Sorry if it is so basic but this much more complicated than swift. :/

variable_assignment.mm
bool hasBonus;
int powerPoints = 5;

if (powerPoints < 3) {
  hasBonus = true;
} else {
  hasBonus = false;
}

3 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

It seems to me you have to swap the true/false assignments, as you don't get a bonus when you have less than three points. More than/equal 3 points will get you the bonus.

variable_assignment.mm
bool hasBonus;
int powerPoints = 5;

if (powerPoints < 3) {
  hasBonus = false;
} else {
  hasBonus = true;
}

Hope that helps :)

Edit

Wow, this doesn't seem to pass! I have no clue what's wrong here...?

Edit 2

Ok, this passes:

variable_assignment.mm
bool hasBonus;
int powerPoints = 5;

if (powerPoints < 3) {
  hasBonus = NO; // FALSE also works
} else {
  hasBonus = YES; // TRUE also works
}

So the assignment is a bit misleading here:

Change the value of powerPoints to 5. Then create an if else statement such that when 'powerPoints' is less than 3 'hasBonus' is false, but in all other cases 'hasBonus' is true.

It seems that code check uses Objective-C, which includes objc/objc.h where YES and NO are defined. But shouldn't that be type BOOL then? However, stdbool.h is most likely included via CoreFoundation in Obj-C, so false and true should also be available, shouldn't they? I mean, even TRUE and FALSE are available.

Gabe Nadel, would you mind explaining that very shortly? My C skills seem to be too limited to answer that :(

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

Apologies to you both. There was a typo in the precompile that checks this code challenge. I have just fixed it and now Martin's first response will pass. Thanks for helping to keep Treehouse neat and tidy!

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Yeah, that was quick! Thanks for your response and fixing this, makes me feel a lot better ;)

It works now, thank you all ! :)