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 trialJericoe States
3,262 PointsCan I say bool "inBounds!(point.X <0 && point.Y > Height)" or does ! only work with || ?
Can I say bool "inBounds!(point.X <0 && point.Y > Height)" or does ! only work with || ?
1 Answer
Steven Parker
231,198 PointsYou can't put an operator between a method name and the parentheses used for calling it. But you can combine any logic operators (using proper syntax) in the expression as you need.
But also consider that you may just need to invert a test itself. For example, the opposite of "<
" is ">=
".
Jericoe States
3,262 PointsJericoe States
3,262 PointsMay have miscoded that//
In the previous video, I learned..
bool inBounds(point.X >= 0 && point.X < 8 && point.Y >= 0 && point.Y < 5); -- example 1 bool inBounds(!point.X < 0 || point.X > 8 || point.Y < 0 || point.Y > 5); -- example 2
Question is, can the "IS NOT operator - !" work with the "AND operator - &&" in example 1 or like in the example 2 above, do we only use the "IS NOT operator - !" with the "OR operator - ||"?
Steven Parker
231,198 PointsSteven Parker
231,198 PointsSorry if I wasn't clear. Yes, you can use any logic operators together, as long as you observe proper syntax. But you also need to bear in mind that operators have an order of precedence and inversion is done before comparison unless you use parentheses to make it otherwise. To make example 2 work, you could do this:
bool inBounds = !(point.X < 0 || point.X > 8 || point.Y < 0 || point.Y > 5);
Notice I also added an assignment operator. I assume it was omitted since it did not make sense to put a "bool" declaration in front of a function call.
Also, when posting code, use Markdown formatting to preserve the code's appearance as I have done here.