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

C#

Why not use a if statement, wouldn't that be cleaner/easier to read?

is there a reason that the video used:

bool inBounds = point.X >= 0 && point.X < Width && point.Y >= 0 && point.Y < Height;

Instead of using it as an IF statement?

1 Answer

Steven Parker
Steven Parker
231,248 Points

Since the objective is to assign a boolean value to "inBounds", it makes the code more compact to assign it directly with the result of the evaluation.

Technically, you could get the same result by doing the evaluation in an "if" expression and then assign a true or false value, but it takes more code. And some might say that the single-statement version is easier to read as well, but that's probably based on more familiarity with comparison and logic expressions.