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!

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


Anatolii Tcai
Anatolii Tcai
2,401 Points

if-else syntax

So is it more preferable to write all the else statements on the same line with the closing parenthesises, as shown in 1? Or I can use the new line for each new statement, as I wrote in 2?

  1. if x > 0 { // do something } else { // do something else }

  2. if x > 0 { // do something again } else { // and again, do something different }

1 Answer

I almost never write an if-else on a single line because it is hard-to-read, but I do do this sometimes when only using a if statement with one line of code as the body.

P.S.: Don't mistake the closing curly brace with the closing parentheses! The } character is the closing brace and ) is the closing parentheses. Notice the closing brace is a little squiggly. :laughing:

I hope this helps. :grin:


Anatolii Tcai
Anatolii Tcai
2,401 Points

Thanks for the answer. For some reason, my message is not shown here as it should be, but I hope you've got the idea.

About the closing curly brace - I'll remember that, Thanks

Extra note: There are three forms of parentheses.

First, there's parentheses. Duh. :laughing: They Look like this: ()

Second, there's curly braces or curly brackets, whatever you call them. They look like this: {}

Lastly, there's brackets or square brackets, whatever you prefer calling it. They look like this: []

Notice the parentheses are round, the braces squiggly, and the brackets look boxy (like a square).