Why semicolon is not required after a curled bracket?

只愿长相守 提交于 2019-12-10 02:11:47

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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?




回答5:


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.




回答6:


Putting a semicolon in is the same effect as

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

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




回答7:


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

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



回答8:


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.




回答9:


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.




回答10:


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.



回答11:


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




回答12:


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


来源:https://stackoverflow.com/questions/5561869/why-semicolon-is-not-required-after-a-curled-bracket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!