Are there any exceptions in removing curly braces for an "if" statement?

I am a computer science student, and some time ago our professor explained us that in C language we can remove curly braces when there's only one statement like:

if (a)
  do b

but we can't do something like this:

if(a)
  do b
  do c

because that would be doing more than one statement.

But it also told us that there's an exception about removing curly braces, something we can't do even if it's only one statement. I did a lot of search but the only thing I found is that I can't do that in a do-while loop, but we are talking about if statements, any help?

Edit: we were also talking about nested if statements, maybe it's about that?

You professor is probably talking about a situation like this:

if (a)
    if (b)
        printf("a and b\n");
else  // this goes with the inner "if"
    printf("not a\n");

Contrary to what the indentation suggests, the else is not associated with the outer if statement but with the inner if statement. In this case you need to add curly braces to the body of the outer if for the else to be associated properly:

if (a) {
    if (b)
        printf("a and b\n");
}
else
    printf("not a\n");

It's best to always use braces for the bodies of conditional and looping constructs to prevent this kind of ambiguity and the bugs that go with them.

Remove curly brace in Java, Between the characters there are no spaces. I'm trying to remove the curly braces and replace them with white space as follows: String s =  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

But it also told us that there's an exception about removing curly braces, something we can't do even if it's only one statement.

I believe your professor is trying to teach you best practices. Even though we can write if statements without curly braces as far as the C language is concened, we should never do so. It is not really a subjective, personal style preference, but a matter of program safety.

Because it is very easy to write bugs like this:

if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    goto fail;
    goto fail;  /* MISTAKE! THIS LINE SHOULD NOT BE HERE */

The 2nd goto will always be executed, which wasn't intended. I dare to state that every single C programmer out there who allows this style, has also written this bug. I sure did now and then myself, before I stopped using that style entirely.

For this safety reason, well-known coding standards such as CERT-C and MISRA-C ban the use of if etc without curly braces. See CERT-C EXP19-C and MISRA-C:2012 rule 15.6.

Learning Rails 3: Rails from the Outside In, Curly braces Spend any bit of time with JavaScript and the use of (what some might You can safely remove curly braces from your code, but be sure to use There is one exception: if there isn't an argument, you'll need a set of empty  Hi Everyone, in the below "xyz (Exception e)" part after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or more newlines between the curly braces.

Well, it is always possible to use a single statement without braces. But what if you don't have any statements:

if (ham)
    int eggs = spam();

does not compile because if needs to guard exactly one statement.

if (ham) {
    int eggs = spam();
}

does. There is one compound statement with a declaration with an initialization that calls a function; and 0 statements. Not really useful as such of course, because the variable is unused otherwise...

Java Programming, Trying Code and Catching Exceptions 525 In object-oriented terminology, you some that might cause exceptions l A closing curly brace You usually code at Cengage Learning reserves the right to remove additional content at any time if  But it also told us that there's an exception about removing curly braces, something we can't do even if it's only one statement. I believe your professor is trying to teach you best practices. Even though we can write if statements without curly braces as far as the C language is concened, we should never do so. It is not really a subjective, personal style preference, but a matter of program safety.

It is difficult to debug when you use macros(multi line) with this syntax. You have to look at preprocessed c code to identify the problem.

#define MULTISTATEMENT a=5;\
                       b=5;
#include <stdio.h>

int main() {
   int a = 10, b = 10;
   if(a == 10)
       MULTISTATEMENT
   else
       a = 3;
   printf("a = %d b = %d\n", a, b);
   return 0;
}

This code generates the error. Place {} either around the macro or around if statement to compile this code.

Remove Curly-braces in try, catch, finnaly blocks, Remove Curly-braces in try, catch, finnaly blocks Catch and re throw exceptions is very slow on JVM, so usually there should be only one catch block on the top level and it And I could not see any kind of solution to it. Alternatively referred to as an open brace, close brace, and squiggly brackets, curly brackets are found on the same keys as the open bracket ( [ ) and close bracket ( ] ) on US keyboards.

Java: Control structures should use curly braces, "Externalizable" classes should have no-arguments constructors. Bug Inner classes which do not reference their owning classes should be "static". Code Smell Exception handlers should preserve the original exceptions. Code Smell Unnecessary imports should be removed. Code Smell  Most software out there isn't, as they say, rocket science. Sure, a few exceptions (say, SpaceX software). Most of the software encodes business practices, and most business practices are a weird arcane set of rules that have no basis in math or science, but is just how some company works. You write that in code, no matter how illogical it is.

How To Remove Curly Braces From String In Java, The segment of code between an opening and closing curly braces is called a code block. There is no method to replace or remove last character from string, but we can do it I was trying to do in this way but it give me exceptions as java. Remove all; Disconnect; The next video is starting stop. Loading Watch Queue After 2 long yearssss they finally removed my braces and now I have perfect teeth !!!! Subscribe:

Clean up entries does not remove curly braces around titles · Issue , I have Windows 7, Jabref 2.11.1, up-to-date Java and there are no exceptions thrown. there are two basic reasons to always use curly braces. 1) it allows multi-line inclusion within an if statement 2) it shows the user the logical separation of code for understand-ability sake.

Comments
  • Never heard of or ran into any situation where {} was required for an if statement.
  • I'm pretty sure you can also remove the braces of a do-while loop.
  • Perhaps your professor's exception is stylistic rather than for syntactic reasons.
  • en.wikipedia.org/wiki/Dangling_else#C
  • a variable declaration like int n = 1; is a single statement, but if placed in curly braces { int n = 1; } it belong to another context and it not accessible from the rest of that context.
  • This is called the dangling else ambiguity and compilers are built to match the else with the closest if.
  • Alright that seem very good, and it's probably that; but maybe something that gives a compile error? Edit: I was also thinking about declaring a variable on a "statement-only-if", that would just basicly destroy the new variable, making it useless.
  • @cmaster Ok, thanks. I'm pretty sure it's the dangling else then, since we were also talking about nestes if statements.
  • @WhiteNoise Btw: google "goto fail bug" for a high-profile, real world example ^^
  • @WhiteNoise Forget about my comment about compiler error not possible with an if(). It's wrong. See Antti Haapala's answer (stackoverflow.com/a/53284483/2445184) for details. I'm going to delete my wrong comment now.
  • Excellent, you actually found a syntactical impossibility :-)
  • @cmaster but it is not "only one statement" ;)
  • Isn't a variable definition also a single statement? Just not the type of statement that's allowed as an if() body?