为什么大括号不是强制的(如下所示)?

huangapple go评论65阅读模式
英文:

Why are braces not mandatory (as shown below)?

问题

现在每个人都在说,如果你想在控制语句内部使用多个语句,就必须将它们放在花括号里面。然而,如果你像这样使用:

if (condition) //第一个if语句
if (condition) //嵌套在第一个if内部的if语句
System.out.println("test"); //第二个if语句内部的语句

这段代码应该会出现错误,因为第二个if语句(没有分号)位于第一个if语句的外部,而没有花括号,这完成了第一个if语句的限制。因此,println语句的作用域应该超出第一个if语句。因此,上述代码应该等同于:

if (condition) {
    if (condition) 
}
System.out.println("test");

但事实并非如此。第一个代码可以成功运行。为什么呢?

英文:

Now everyone speaks that if you wanna use multiple statements inside a control statement, it is mandatory to include them inside braces. However if you use like this:

if(condition) //first if statement
if(condition) //if statement nested inside the first
System.out.println("test"); //statement inside the second if statement

The code should result an error as the second if statement (without the semicolon) is inside the first one without the braces which completes the first if statement's limit. And hence the scope of the println statement should be out of the first if statement. Therefore the above code should be equivalent to

if(condition) {
    if(condition) 
}
System.out.println("test"); 

But it's not like that. The first code runs successfully. Why?

答案1

得分: 4

因为条件语句包括其限定的语句。

花括号不是 if 语句语法的一部分。它的语法只是:

 "if" "(" <expression> ")" <statement>

或者

"if" "(" <expression> ")" <statement> "else" <statement>

一种类型的语句是块语句,其语法是:

"{" <statement>... "}"

这里的 <statement>... 表示 "零个或多个 <statement>"。

块语句可以包含 if 语句(或任何其他类型的语句)。但它不能将语句一分为二。而 if (condition) 只是语句的一半。

英文:

Because a conditional statement includes the statement it qualifies.

Braces are not part of the syntax of the if statement. It's syntax is simply:

 &quot;if&quot; &quot;(&quot; &lt;expression&gt; &quot;)&quot; &lt;statement&gt;

Or

&quot;if&quot; &quot;(&quot; &lt;expression&gt; &quot;)&quot; &lt;statement&gt; &quot;else&quot; &lt;statement&gt;

One type of statement is the block statement, whose syntax is:

&quot;{&quot; &lt;statement&gt;... &quot;}&quot;

Where &lt;statement&gt;... means "zero or more &lt;statement&gt;s".

The block statement can include an if-statement (or any other kind of statement). But it can't chop a statement in half. And if (condition) is only half of a statement.

答案2

得分: 1

您的问题以以下陈述开头:

"现在大家都说,如果您想在控制语句内部使用多个语句,那么必须将它们包含在大括号内。"

这绝对是正确的。您在这里遇到的微妙之处是某事被视为单个语句与被视为多个语句之间的区别。

正如 @rici 指出的,大多数编程语言中,if语句包括某种条件表达式和一个受保护的语句块,用于在条件为真时执行。从这个意义上说,if语句必须在其内部包含一些语句,以保持语法正确。

所以,让我们自下而上看看您的陈述。这个语句:

System.out.println("Test");

被视为单个语句。同样地,

if (condition)
System.out.println("Test");

也是一个单一的条件语句。然而,

if (condition)

独立存在时并不是一个有效的条件语句;这将是一个语法错误。

这意味着

if (condition1)
if (condition2)
System.out.println("Test");

是一个单一的条件语句。具体来说,它是一个条件,其条件是 condition1。该语句也是一个条件语句。该条件语句的条件是 condition2,并保护了打印语句。

英文:

You began your question with this statement:

> Now everyone speaks that if you wanna use multiple statements inside a control statement, it is mandatory to include them inside braces.

That’s absolutely true. The nuance you’re encountering here is what it means for something to be a single statement as opposed to multiple statements.

As @rici noted, in most programming languages an if statement consists of both some sort of conditional expression and a guarded statement to execute if that condition is true. In that sense, an if statement has to have some statement inside of it to be syntactically correct.

So let’s look at your statement, but do it bottom-up. The statement

System.out.println(&quot;Test&quot;);

is treated as a single statement. Similarly,

if (condition)
System.out.println(&quot;Test&quot;);

is also a single conditional statement. However,

if (condition)

on its own is not a valid conditional statement; this would be a syntax error.

This means that

if (condition1)
if (condition2)
System.out.println(&quot;Test&quot;);

is a single conditional. Specifically, it’s a conditional whose condition is condition1. The statement is guards is also a conditional. That conditional has condition condition2 and guards the print statement.

huangapple
  • 本文由 发表于 2020年9月5日 03:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63746850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定