在大多数 C 派生语言(C、Java、Javascript 等)中,for
循环具有相同的基本语法
for (int i = 0; i < 100; i++) {
// code here
}
为什么这种语法包含分号,而分号通常保留在行尾?另外,为什么后面没有分号i++
?
在大多数 C 派生语言(C、Java、Javascript 等)中,for
循环具有相同的基本语法
for (int i = 0; i < 100; i++) {
// code here
}
为什么这种语法包含分号,而分号通常保留在行尾?另外,为什么后面没有分号i++
?
这个伪代码:
for (A; B; C) {
D;
}
可以在内部转换为
{ // scope bracket
A;
while (B) {
D;
C;
}
}
分号不用于结束行。用于结束指令。在大多数这些语言中,您可以执行以下操作:
int i;i = 0;
这是合法的。寻找任何缩小的 Javascript 代码。您会看到每行有数千个分号。
同样的原理,一个 for 块需要三个指令。它们用分号分隔,以便编译器或解释器知道每个命令的开始和结束位置。
这是完全合法的(尽管它会导致无限循环):
for (;;) {}
I feel this can be answered sufficiently by examining how language parsers resolve the syntax. For example, a common depiction of a for loop is:
for (initialization; condition; increment-decrement) {
/** statements **/
}
You can generalize that to:
for (expression; expression; expression) {
/** statements **/
}
Note that the generalization is not entirely accurate, because the middle expression is typically reserved only for relational expressions, and the other two are either statements or statement lists. For example, in C and C++ you can have multiple statements in the initializer or increment-decrement regions by using the comma (,) operator.
It may help to note that statements are usually a collection of zero or more expressions, often separated by operators.
In many languages, a semicolon doesn't occur at the end of a line of code, it typically occurs at the end of a statement. A statement is a often defined as the smallest standalone executable element of a piece of code. A common type of statement is an expression statement, which is a statement composed of exactly one expression. This helps to explain why there is a semicolon at the end of each expression in the for loop construct, because it is consistent with how the language parser interprets statements.
There is no semicolon at the end simply because that's how the language grammar is defined. The other components, as mentioned above, could be for consistency.
This is difficult to answer, but I think a probable reason is that it's consistent with how it has been done in the past. C was/is a very popular language, and many languages base their syntax on some variant of C including C++, C#, Objective-C, Java, Python, Perl, and JavaScript.
可能没有合理的解释来解释为什么会出现这种特定的for(;;){}
语法。你应该问 Kernighan 或 Ritchie。
回到 60 年代编程语言的历史,花括号可能出现在 BCPL 编程语言中,而括号包裹的条件则被 B 美化(它只有一个while
语句, no for
)。C 以 B 为模型。
自 1972 年以来,C 已经渗透到计算机工程和后续语言的所有领域,这些领域通常以 C 语法(C++、Java、Javascript、C#、Scala 等)为模型,而不是破坏已建立的程序员的习惯。这包括for(;;)
循环语法和花括号。
作为旁注,有许多广泛使用的语言不采用 C 样式语法,例如 Python,for
您可能会发现其循环更多逻辑(显然,这是个人意见),或 Ruby。
The general form is:
for ( expression; expression; expression ) { ... }
The parser can easily recognize these expression because their syntax is identical to the sintax of "normal" expressions:
{
expression;
expression;
...
}
The last expression can easily be recognized because it ends with a ')'. Furthermore, commas couldn't be used because they can be put inside single expressions:
for ( i=1,j=10; i<10,j>0; i++,j--) { ... }
for
是一个语言关键字/指令 - 不要与对库函数的调用混淆。
关键字/指令是语言的一部分;if、while、do、for 等。因此,编译器需要在编译、解释期间生成汇编代码(或某种指令代码)。当用for
c 语言开发指令时(由 Brian Kernighan 和 Dennis Ritchie 编写),他们必须选择 for 操作的语法,以及如何将操作分解为汇编程序
从:
for( start-condition ; end-condition ; step-control )
像这样:
mov eax, $x
beginning:
cmp eax, 0x0A
jg end
inc eax
jmp beginning
end:
mov $x, eax
这种语法随后在 C++ 中使用,其他语言也紧随其后。
在 c 中,分号是行/命令终止符。在其他语言中,它是行分隔符。在 for 循环中,它是术语的分隔符。
在 c 中,并非所有术语都是必需的:
for(;;)
是有效代码。并且等价于while(1)
(pseudo: while(true))
在 Pascal/modular2 中,结构不同
for i:= start_value to end_value do