I\'m almost certain this has been asked before, but I can\'t find it being answered anywhere.
When can I omit curly braces in C? I\'ve seen brace-less return
The following seems a little tricky:
if (...)
printf("I'm conditional\n");
I guess the C preprocessor takes care of empty lines, so this statement is fine. Very bad practice of course.
Anytime there is a block with more than one line (counting lines as a single statement with a ; )
Examples:
for (i = 0; i < j; i++ )
foo(i);
while (1)
foo();
if( bool )
foo();
void cheese()
printf("CHEESE");
Everything after the ; in the above lines does not count as 'inside' the block as if there were { }.
If in doubt, use braces. They don't "cost" any extra [unless you absolutely use them incorrectly].
A fairly basic rule is "if you have more than one semicolon at the same indentation, you should have braces". It's not entirely that simple, but as a "one sentence with no ifs, buts or other complications" it will have to do.
And yes, Java being based on C-syntax has the same fundamental rules - there are possibly some weirdness where Java is different from C or C++, but that's unusual/strange things, not something you'd normally hit when writing typical code [I haven't done much java, so I can't say I've found any of them, but I'm pretty sure there are SOME differences - they are different languages, after all].
You can omit them when there's a single statement you're executing:
for(...)
printf("brackets can be omited here");
if(...)
printf("brackets can be omited here");
else
printf("brackets can be omited here too");
etc.. You can always add them in, it never hurts and help to clarify the scope, but you're pretty safe. The only "catch" I can think of is if you attempt to make a declaration in that scope (which is useless by the way):
if(...)
int a = 5;
This will cause an error, but that's because you can perform a single statement without curly brackets, not a declaration. Important distinction.
Technically speaking, you can even do more than one operation without brackets if they're concatenated with the ,
operator... not that I advice doing it, just worthy of note:
if(...)
value++, printf("updated value with brackets");
And to your edit, you can see in the java specification that the rules are the same. I linked specifically to the if
section, but you can see after the if
it's expected a statement, thus you can skip the curly brackets.
The only places you can omit brackets are for the bodies of if-else
, for
, while
, or do-while
statements if the body consists of a single statement:
if (cond)
do_something();
for (;;)
do_something();
while(condition)
do_something();
do
do_something();
while(condition);
However, note that each of the above examples counts as single statement according to the grammar; that means you can write something like
if (cond1)
if (cond2)
do_something();
This is perfectly legal; if (cond2) do_something();
reduces to a single statement. So, for that matter, does if (cond1) if (cond2) do_something();
, so you could descend further into madness with something like
for (i=0; i < N; i++)
if (cond1)
if (cond2)
while (cond3)
for (j=0; j < M; j++)
do_something();
Don't do that.
An example:
int a[2][2] = {{1, 2}, {3, 4}};
you can use the valid equivalent form:
int a[2][2] = {1, 2, 3, 4};
Some verbose compilers may warn, but it is valid.
Note that for the if
statement (same for the while
, the for
statement, ...) the {}
are not omitted. They are just not requested. The if
statement has this syntax:
if (expression) statement
If you want a multiple statement instead of a single statement you can use a compound statement which is surrounded {}
.