When Should I Use a Semicolon After Curly Braces

When should I use a semicolon after curly braces?

You use a semicolon after a statement. This is a statement:

var foo = function() {  alert("bar");};

When do we need to put semicolon after curly brackets? i.e.: };

"Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once."

Java - Anonymous Classes

Instead of doing something like this:

class MyThread extends Thread {
@Override
public void run(){
// something
}
}
...
Thread myThread= new MyThread();

You can sorten your code by doing it this other way using anonymous classes.

Thread myThread=new Thread(){
@Override
public void run(){
// something
}
};

Is always putting a semicolon after an end curly-brace a bad idea?

On a program level, it makes no difference to have this:

function() {

};

Or this:

function() {

}

I do recommend that you pick one way to do it and stick with it for the purpose of consistency.

You should also read through this post:
Do you recommend using semicolons after every statement in JavaScript?
A lot of good points on this subject are made there.

When would you put a semicolon after a method closing brace?

It's allowed by the grammar as a concession to harmless syntax errors, but it's not generally used and doesn't mean anything different (than leaving the semicolon out).

Just as a }; inside a method (such as after an if block) is a null statement and is allowed, an errant semicolon outside a method is considered a null declaration and is allowed.

Specifically, the following production from the Java Language Specification allows this:

ClassBodyDeclaration:
;
[static] Block
ModifiersOpt MemberDecl

When is a semicolon after } mandated in c/c++?

int a[2] = {1,2}, j = 5;

When initialization of array or structure are done with {} all the subsequent variables are declared after ,.

Edit: As you changed your question; ; is mandatory after the class, enum, initialization syntax declarations.

class A {};  // same for `struct
enum E {}; // enum class (c++0x)
int a[] = {1,2}; // array or object initialization

And as per comment by @rubenvb:

do {} while(condition);

semicolon after curly braces in c++

As QtCreator correctly detects, that is definitely an extra semicolon that not only is useless, but can also cause compiler warnings and confusion.

For example GCC with the -Wpedantic flag will trigger:

warning: extra ';'

Semicolon after closing curly bracket in PHP

I think that those 2 semicolons do nothing here. It is probably being interpreted as an empty expression immediately following the if/while.

Why semicolon is not required after a curled bracket?

Because curly brackets are used for grouping statements, but they are not statements themselves.

IDE expects semicolon after curly braces

You are declaring an enum, not a class, so the compiler expects to see enum elements at the beginning. These elements should be separated by , and end with a ;

public enum CLIError implements IUIErrorEnum<CLIError>, IErrorEnum<CLIError> {
ELEMENT_1,
ELEMENT_2,
LAST_ELEMENT;

@Override
public ReaderType getType() {
return ReaderType.CLI;
}
}

Even if you don't declare any enum elements, the ; is mandatory.

Note that if you don't declare a method in the enum, the ; can be omitted.

public enum CLIError {
// compiles without ;
}

or

public enum CLIError {
ELEMENT_1,
ELEMENT_2,
LAST_ELEMENT
// compiles without ;
}


Related Topics



Leave a reply



Submit