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 trialSurya D.
629 PointsAdvantages of a "code block"
What are the advantages of a code block, what is it used for?
3 Answers
manav
5,466 PointsA code block is used when we want to group together a set of statements that are meant to be executed together. A code block is represented by an opening and closing curly brackets.
Adam Brown
12,453 PointsFunctions are code blocks. Creating code blocks makes your program much more flexible in terms of what it can do based on user interaction. As manav said, it's basically just a set of statements that run together.
Lars Reimann
11,816 PointsThe if-statement can only have a single statement in its body. So this won't work as the indentation might suggest:
if (2 < 3)
console.printf("Looks like we");
console.printf("know some math");
Code blocks are a way around this. You can group multiple statements together, so they act like a single one.