riox
riox

Reputation:

Why semicolon is not required after a curled bracket?

I know that a semicolon is required after a statement (I'm talking about Java, C++, and similar languages), but is not required after a curled bracket. Why so?

if (a > b) 
  printf("hello!"); // semicolon is mandatory

if (a > b) {
  printf("hello!");
} // semicolon is not required

What is the reason? I mean, what is the theory behind this?

Upvotes: 13

Views: 6607

Answers (12)

Ira Baxter
Ira Baxter

Reputation: 95402

Most people think of statements as being a simple command, often with a keyword, and some parameters, such as "goto x", "a=y+2", etc. There has to be some indication of where one statement ends and another begins, much like English sentences need to end with a period. Traditionally the grammars of most langauges require semicolons after such statement as such indication.

A { ... } "curly brace pair" is a block, which is a special kind of a statement, but the semicolon isn't needed because the curly braces make the boundaries clear.

Many language also allow ";" by itself, to represent the empty statement. Why would you need one? For the same reason the natural number system requires "zero" instead of "one", and sets can be empty.

But it means you can write:

{ ... } ;

and most langauge compilers accept it without remark. But you should think of it as:

{  ... }
;

and generally there's no good reason to write that.

As a practical matter, languages that accept {} (e.g., "empty brackets") don't need the empty-statement ;, because these are semantically identical. But language designers seem stuck on tradition; have you noticed how every "modern" language seems to be a bad syntactic copy of C?

Upvotes: 3

irreputable
irreputable

Reputation: 45443

This is a fair question. A block is a statement. It is natural to desire uniformity, and wonder why all statements are not terminated the same. There is no technical problem if we do require a ; after a block. But we are also lazy, since } can unambiguously mark the end of a statement, we don't want to have to type another marker.

A related observation: in C++, you must end class declaration with a ;

class A
{
    ...
}; // the semicolon is mandatory!

That annoys the heck of a lot of people. The semicolon is required because the language allows some other stuff after } so the } is not a reliable end marker.

In Java, that's not the case. The } ends the class declaration and that is it. So ; is not needed.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533680

The only place you will need a semi-colon after a close curly bracket is after an array initialization as you could continue the line e.g.

int[] i= { 1,2,3}, j= {1};

The semi-colon is required because the '}' here doesn't tell the compiler where the end of the line is.

Similarly

Runnable r = new Runnable() {
   public void run() {
   }
}, r2 = new Runnable() {
   public void run() {
   }
}; // required as you could define another Runnable or an array of Runnable.

Upvotes: 0

iammilind
iammilind

Reputation: 70040

When you use curly braces to enclose a block of code, you don't need semicolon:

namespace Some
{
  void fun(int i)
  { 
    while(i != 0)
    {
      cout<<"i = "<<i<<endl;
      i --;
    }  // semicolon not needed
  }  // semicolon not needed
} // semicolon not needed

In other cases, you need semicolon (like initialization, declaration etc.):

struct A {
// ... declare members
}; // declaration; put semicolon
int a[] = {0, 1, 2};  // initialization; put semicolon

Upvotes: -1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361622

Note : this answer is specific to the C++ language, not Java.

I would say that the semicolon is not required by the language grammar (2003). This is how the language defines the grammar.

The code which you've written is called Compound statement or block and the language specification (2003) defines the grammar of compound statement in the section §6.3/1 as,

So that several statements can be used where one is expected, the compound statement (also, and equivalently, called “block”) is provided.

compound-statement:  
             { statement-seqopt }

statement-seq:  
            statement
            statement-seq statement

Do you see any semi-colon in the grammar shown above? No. That's why the semi-colon is not required after the curly bracket in your code.

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264601

Because the language is defined as:

statement:
    labeled-statement
    expression-statement
    compound-statement
    selection-statement
    iteration-statement
    jump-statement
    declaration-statement
    try-block

labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

expression-statement:
    expressionopt ; 

compound-statement:
    { statement-seqopt } 

statement-seq:
    statement
    statement-seq statement

selection-statement:
    if ( condition ) statement
    if ( condition ) statement else statement
    switch ( condition ) statement

condition:
    expression
    type-specifier-seq declarator = assignment-expression

iteration-statement:
    while ( condition ) statement
    do statement while ( expression ) ; 
    for ( for-init-statement conditionopt ; expressionopt ) statement

for-init-statement:
    expression-statement
    simple-declaration

jump-statement:
    break ;
    continue ;
    return expressionopt ; 
    goto identifier ; 

declaration-statement:
    block-declaration 

All normal control statements are built recursively from each other. The real work is done by the expression-statement. If you notice the expression-statement is always terminate by the ;. The other statements to watch are the jump-statement.

But the main reason is that they are not needed after the {} block to allow the easy parsing of a statement.

Upvotes: 8

Sarfraz Ahmed
Sarfraz Ahmed

Reputation: 1399

In this case curly brackets are defining a block of statements. Like any other block. while you are about to declare and initialize the array you must provide the ; because in this case you are writing a statement.

Upvotes: 0

Michael Brown
Michael Brown

Reputation: 9153

If you are using the object initialization syntax in C# 3+ a semicolon comes after the bracket

var foo = new Foo
  {
    Bar = "Fizzbuzz"
  };

Upvotes: 0

Kalle
Kalle

Reputation: 13346

The philosphical reasoning aside, down under the hood it's essential that the compiler knows where to separate each command from the next. A bracket is in and of itself a separator, so a semi-colon is unnecessary.

Upvotes: 5

jonsca
jonsca

Reputation: 10381

Putting a semicolon in is the same effect as

if (a > b) {
 printf("hello!");
}printf("Goodbye");

and leaving the printf("Goodbye") part out.

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86406

Because curly is not the statement it is used to group the statements. It does not need a terminator.

Upvotes: -1

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

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

Upvotes: 8

Related Questions