Alex Rivera | Logout

Is it ok if I omit curly braces in Java?

Asked 2011-11-05T12:42:36.417
97

I've searched for this, but couldn't find an answer and for whatever reason I was too ashamed to ask professor, due to that feeling when hundreds of people stare at you...

Anyhow, my question is what's the importance of having brackets? Is it OK if I omit them? Example:

for (int i = 0; i < size; i++)  {
   a += b;
}

vs

for (int i = 0; i < size; i++)
   a += b;

I know both of them will work, but if I omit the brackets (which I tend to do a lot, due to visibility) will that change anything, anything at all? As I said, I know it works, I tested it dozen of times, but now some of my uni assignments are getting larger, and for some reason I have irrational fear that in the long run, this my cause some problems? Is there a reason to fear that?

Edit
Report

1 Answer

4

If you use brackets your code is more readable. And if you need to add some operator in same block you can avoid possible errors

answered 2011-11-05T12:46:52.917

Your Answer