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

Java Java Basics Perfecting the Prototype Censoring Words - Using String Equality

Alexander Oaktree
Alexander Oaktree
515 Points

Why does he open another code block?

Why do I need to open another code block after the new if statement? What would happen if I simply added the if statement without opening a new code block?

2 Answers

In Java, if you write the code

If (condition)

statement1;

statement2;

statement3;

would execute statement1 if the condition is true, but would execute statement2 and statement3 under all circumstances because if there aren't braces around the code block, Java will just assume that the following statement is associated with the if statement and everything else afterwards is not.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hello!

It would unfortunately would create a syntax error, if blocks and most logical blocks begin with

if(condition) {
//do stuff;
}

This was planned out when they created java as a language, so without following the syntax it would probably give you a "is not a statment { is expected" or an error similar to that.