为什么花括号后面不需要分号?
我知道语句后面需要分号(我指的是 Java、C++ 和类似语言),但花括号后面不需要分号。为什么会这样呢?
if (a > b)
printf("hello!"); // semicolon is mandatory
if (a > b) {
printf("hello!");
} // semicolon is not required
原因是什么?我的意思是,这背后的理论是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
因为大括号用于对语句进行分组,但它们本身并不是语句。
Because curly brackets are used for grouping statements, but they are not statements themselves.
因为该语言被定义为:
所有正常的控制语句都是相互递归构建的。真正的工作是由
表达式语句
完成的。如果您注意到表达式语句
总是以;
终止。其他需要注意的语句是跳转语句。但主要原因是在 {} 块之后不需要它们来允许轻松解析语句。
Because the language is defined as:
All normal control statements are built recursively from each other. The real work is done by the
expression-statement
. If you notice theexpression-statement
is always terminate by the;
. The other statements to watch are thejump-statement
.But the main reason is that they are not needed after the {} block to allow the
easy
parsing of a statement.撇开哲学推理不谈,在幕后,编译器知道在哪里将每个命令与下一个命令分开是至关重要的。括号本身就是分隔符,因此不需要分号。
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.
大多数人认为语句是一个简单的命令,通常带有关键字和一些参数,例如“goto x”、“a=y+2”等。必须有一些指示一个语句结束而另一个语句开始的位置,就像英语句子需要以句点结尾一样。传统上,大多数语言的语法都需要在此类语句之后使用分号作为指示。
{ ... } “大括号对”是一个块,它是一种特殊的语句,但不需要分号,因为大括号使边界清晰。
许多语言也允许使用“;”单独表示空语句。为什么你需要一个?出于同样的原因,自然数系统需要“零”而不是“一”,并且集合可以为空。
但这意味着你可以写:
并且大多数语言编译器都接受它而不加注释。但您应该将其视为:
并且通常没有充分的理由这么写。
实际上,接受 {}(例如“空括号”)的语言不需要空语句 ;,因为它们在语义上是相同的。但语言设计者似乎仍固守传统;您是否注意到每种“现代”语言似乎都是 C 语言的糟糕语法副本?
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?
这是一个公平的问题。块是一个语句。渴望一致性是很自然的,并且想知道为什么所有语句的终止方式不同。如果我们确实需要在块后添加
;
,则不存在技术问题。但我们也很懒,因为}
可以明确地标记语句的结束,所以我们不想键入另一个标记。一个相关的观察:在 C++ 中,您必须以
;
结束类声明,这让很多人烦恼。分号是必需的,因为该语言允许在
}
之后添加一些其他内容,因此}
不是可靠的结束标记。在 Java 中,情况并非如此。
}
结束了类声明,仅此而已。所以不需要;
。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
;
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.效果相同。
放入分号与去掉
printf("Goodbye")
部分的Putting a semicolon in is the same effect as
and leaving the
printf("Goodbye")
part out.如果您使用 C# 3+ 中的对象初始化语法,则括号后面会出现一个分号
If you are using the object initialization syntax in C# 3+ a semicolon comes after the bracket
在这种情况下,大括号定义了一个语句块。就像任何其他块一样。当您要声明并初始化数组时,您必须提供 ;因为在这种情况下你正在写一份声明。
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.
注意:这个答案特定于 C++ 语言,而不是 Java。
我想说,语言语法 (2003) 不需要分号。这就是语言定义语法的方式。
您编写的代码称为复合语句或块,语言规范 (2003) 在第 §6.3/1 节中将复合语句的语法定义为:
您在上面所示的语法中看到任何分号吗?不。这就是为什么代码中的大括号后面不需要分号。
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,
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.
在右花括号后面唯一需要分号的地方是在数组初始化之后,因为您可以继续该行,例如
需要分号,因为这里的“}”不会告诉编译器该行的结尾在哪里莱恩群岛
相似地
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.
The semi-colon is required because the '}' here doesn't tell the compiler where the end of the line is.
Similarly
因为 curly 不是语句,所以它用于对语句进行分组。它不需要终结者。
Because curly is not the statement it is used to group the statements. It does not need a terminator.
当您使用大括号括起代码块时,不需要分号:
在其他情况下,您需要分号(如初始化、声明等):
When you use curly braces to enclose a block of code, you don't need semicolon:
In other cases, you need semicolon (like initialization, declaration etc.):