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#

He's doing way more than what he says "replace the if and else ...with.." Can anyone explain the process?

He's doing more than replace the "if and else and their curly braces." He's also deleting content. Can anyone explain?

Steven Parker
Steven Parker
231,264 Points

Please provide a link to the page with this video, and an approximate time index where this happens.

https://teamtreehouse.com/library/c-objects/encapsulation-and-arrays/ternary-if

My first question was, did he mean replace only the if and else and the curly braces, or did he mean the content between the if and else. Then he proceeds to delete random content between the if and else, adds and underscore character, and then a ? without explaining what he is doing. If I was already fluent in this language I would get what he is saying, but since I'm not all I have to rely on is what he says and does, and when they don't match up...confusion!

2 Answers

Steven Parker
Steven Parker
231,264 Points

No significant content is being removed. And the underscores were already there to begin with.

Besides the "if and else and their curly braces" he also removes a semicolon and two return statements. But then he puts one return back in. Only one is needed after the conversion because it will now return the proper value from either condition. This is because "if...else" chooses between complete statements and a ternary chooses between expressions.

The question mark and colon are the symbols that represent the ternary operation, and are what he is replacing the other components with to make the conversion.

He deleted "if".... { return } else { return ....

He added a "?" at the end of (pathStep < _path.Length), replaced a ; with a :

deleted } else } return

All this seems totally random and way more than "replacing if and else and their curly braces with the question mark and colon characters." If I did literally what he said, I would get this:

original: { if(pathStep < _path.Length) { return _path[pathStep]; } else { return null; }

" replacing if and else and their curly braces with the question mark and colon characters."

{

 ? (pathStep < _path.Length)
    return _path[pathStep];
       return null;

...right?

but he doesn't replace "if " with a "?" ...he deletes the if, and add a "?" at the end of the line, he proceeds to delete "return" (which he made no mention of), then he deletes else and return (made no mention of deleting return), however I can see that the : is replacing else in this case.

This is very confusing.

Steven Parker
Steven Parker
231,264 Points

I agree that the explanation is a bit abbreviated. The whole process is more like this:

  • the "if" in front of the conditional is replaced by "?" after the conditional
  • the braces around both statements are removed
  • the statements are converted into expressions (by removing "return" and the semicolons)
  • the "else" is replaced by ":"
  • the ternary expression is converted into a statement by adding a "return" in front and a semicolon at the end

Thanks for breaking it down.