' if'陈述

发布于 2025-01-28 20:44:48 字数 8 浏览 3 评论 0 原文

continue

Today, after half an hour of searching for a bug, I discovered that it is possible to put a semicolon after an if statement instead of code, like this:

if(a == b);
// Do stuff

Which basically means that the stuff will be done whether a equals b or not, and the if statement has no point whatsoever. Why doesn't Java give me an error? Is there any situation in which this would be useful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

情绪 2025-02-04 20:44:48

为什么会发生?

java语言规范

空语句

一个空语言无能为力。

 空陈述:
    ;
 

空语的执行总是正常完成

It essentially means that you want to execute empty statement if a==b

if(a == b);

What should you do:

There are two main solutions to this problem:

  1. You can avoid problems with empty statement by using code格式化
    {和} 中的周围内容 。这样做
    您的空语言将更可读。

      if(a == b){
      ;
    }
     
  2. 您还可以检查用于静态代码分析的工具,例如:

    “”

    他们可以立即突出此类问题。

我建议将这两种解决方案结合起来。

Why does it happen?

Java Language Specification says that:

The Empty Statement

An empty statement does nothing.

EmptyStatement:
    ;

Execution of an empty statement always completes normally

It essentially means that you want to execute empty statement if a==b

if(a == b);

What should you do:

There are two main solutions to this problem:

  1. You can avoid problems with empty statement by using code formatter
    and surrounding stuff inside if with { and }. By doing this
    Your empty statement will be much more readable.

    if(a == b){
      ;
    }
    
  2. You can also check tools used for static code analysis such as:

    They can instantly highlight problems such as this one.

I would recommend to combine both solutions.

如歌彻婉言 2025-02-04 20:44:48

是否有任何有用的情况?

有用?如“使您的代码更清洁,更清晰,更快,更可维护”?一点也不。这很可能很差,令人困惑的代码。

,但不一定是良性。由于方法会导致副作用,因此可以执行动作和/或更改状态,并可以选择评估 Short-short--操作员的电路

if( a() && b() );

在这里, a() b()可能会做某事, b()仅在 a()才能执行代码>是正确的。

至于为什么,我认为答案仅仅是偏离定义的预期行为会更糟(例如之类的语句while(reader.read()); )而不是编写不良代码的开发人员的替代方案。

编写不良代码总是可能的。只是要重申,这几乎在任何情况下都是不好的代码。

Is there any situation in which this would be useful?

Useful? As in "makes your code cleaner, clearer, faster, more maintainable"? Not at all. This is most likely poor, confusing code.

But it's not necessarily benign. Such a statement can perform actions and/or alter state due to methods which cause side effects, and optionally evaluate those methods due to short-circuiting of operators.

if( a() && b() );

Here, a() or b() may do something, and b() will only execute if a() is true.

As to why, I think the answer is simply that it would be worse to deviate from defined, expected behavior (e.g. statements like while(reader.read());) than the alternative of developers writing bad code.

Writing bad code is always possible. And just to reiterate, this would be bad code in almost any case.

流年已逝 2025-02-04 20:44:48

可能的用例:

if (a==b);
else {
  // Do something
}

不好,但可能。

尽管如此,我确实认为,如果,Java规范应该禁止空

A possible use case:

if (a==b);
else {
  // Do something
}

Not good, but possible.

Still, I do think that the Java specification should disallow an empty if.

薄情伤 2025-02-04 20:44:48

如果您使用的是Eclipse,则可以警告您有关这些陈述的信息:

“ Compiler-> compiler-> erriver-> errors-> errors/警告”>

If you're using Eclipse, you can make it warn you about those statements:

Java->Compiler->Errors/Warnings

痴情 2025-02-04 20:44:48

如果使用语句使用,则如果条件为真,则将执行之后的第一个语句。如果您在(带卷曲括号)之后有一个块,则对整个块很重要。如果没有块,则仅计算一个语句。单个半龙是一个空的语句。您也可以从您的示例中编写代码:

if(a==b) {
    ;
}

If you use an if statement, the first statement after the if will be executed if the condition is true. If you have a block after the if (with curly braces), it counts for that whole block. If there is no block it counts for only one statement. A single semicolon is an empty statement. You could also write the code from you example like this:

if(a==b) {
    ;
}
浅沫记忆 2025-02-04 20:44:48

这是从句法有更多的句法糖与陈述区分表达式的日子。

基本上,逗号用作列表项目分离器,因此将半隆用作“语句列表”分隔符。缺点是在列表中处理空项目的处理以及块中的null语句。

在项目列表中,Java使用明确的关键字 null ,但是“ null语句”只是一个空行。允许存在空线是从C继承的传统中保留了C。

为什么这样做?特别是使用 语句时,当您知道没有执行任何语句时:因为某些语句具有副作用:

 int c;
 if ((c = in.read()) != -1);

是的,这不是最好的例子,但基本上它说从流中读取字节什么都不做。在某些角落情况下可能很有用,但是即使此示例不是最好的,它也说明了意图。我们希望在不小心执行任何陈述的情况下感受表达的副作用。

It is an old leftover from the days when there was more syntactic sugar to differentiate expressions from statements.

Basically, the comma was used as the list item separator, so the semicolon was used as the "list of statements" separator. The downside is in the handling of null items in lists, and null statements in blocks.

In a list of items, Java uses the explicit keyword null, but a "null statement" is just an empty line. Allowing the existence of an empty line is a holdover from tradition inherited from C.

Why do it? Especially with an if statement when you know that no statements are being executed: Because some if statements have side effects:

 int c;
 if ((c = in.read()) != -1);

Yes, it is not the best example, but basically it says read a byte from the stream and do nothing. Might be useful in some corner cases, but even if this example isn't the best, it illustrates the intent. We want to feel the side-effects of the expression without accidentally executing any statements.

温柔嚣张 2025-02-04 20:44:48

我想不出有用的场合。 在IDE中使用代码格式之类的循环可能很有用

 while(do something);

它对于诸如循环或

 for(init; do something; something else);

,这些错误变得很明显。有些IDE也将其强调为可能的错误。

I can't think of an occasion where it is useful. It can be useful for loops like

 while(do something);

or

 for(init; do something; something else);

If you use your code formatting in your IDE regularly these sort of bugs become obvious. Some IDEs highlight this as a probable bug as well.

音栖息无 2025-02-04 20:44:48

我同意您的看法,对于人类来说,这没有任何有用的目的。我怀疑它在那里,因为它简化了语言定义。这意味着在 e上与 while 时的事物相同的事物。

I'd agree with you there's no useful purpose to this for a human. I suspect it's there because it simplifies the language definition; it means that the thing that comes after an if is e same as the thing that comes after a while, for instance.

软甜啾 2025-02-04 20:44:48

Java允许一个空块任何位置允许语句块。我敢肯定,将此作为所有块的一般规则简化了编译器。

我同意这主要是造成很难找到的错误的原因。即使有一个语句,我也总是在块周围使用牙套,但是Java允许您在任何时候都用牙套制作一个块,因此使用牙套无法将您从这种命运中保存下来。例如,我曾经浪费了4个小时尝试找到这样的东西:

while (condition);
{
    statement;
    statement;
}

第一行末尾的半隆是错字,意外地将声明块用于时循环空的。由于语法有效,因此编译并运行良好,只是我想要的方式。确实很难找到

我可以想到一种情况,即非常好,您可以拥有空块,这是这样的:

if (condition1) {
    do_action_1();
}
else if (condition2) {
    //nothing really to do in this case
}
else if (condition3) {
    do_action2();
}
else {
    do_action3();
}

在上面的示例中,您希望能够分开各种条件。请记住,这些条件可能是重叠的,因此并非总是可以重新安排订单。如果其中一个条件确实不需要任何事情,那么Java允许您拥有一个空块是很好的。否则,当您真的不想完成任何操作时,该语言将需要某种形式的“ NOOP”方法。

我个人更喜欢明确的“ NOOP”语句 - 但这不是Java的定义方式。

Java allows an empty block any place a statement block is allowed. I am sure making this a general rule for all blocks simplifies the compiler.

I agree that this is primarily the cause of bugs that are spectacularly hard to find. I always use braces around blocks, even when there is a single statement, but Java allows you to make a block with braces at any point, so using braces can not save you from this fate. For example, I once wasted 4 hours trying find something like this:

while (condition);
{
    statement;
    statement;
}

The semicolon at the end of the first line was a typo, accidentally making the statement block for the while loop empty. Because the syntax is valid the program compiled and ran fine, just not the way I wanted it to. It was really hard to find.

I can think of one situation where it is very nice that you are allowed to have empty blocks, and this is something like this:

if (condition1) {
    do_action_1();
}
else if (condition2) {
    //nothing really to do in this case
}
else if (condition3) {
    do_action2();
}
else {
    do_action3();
}

In the above example, you want to be able to separate out various conditions. Remember, those conditions might be overlapping, so it is not always possible to rearrange the order. If one of the conditions really does not need anything done, then it is nice that Java allows you to have an empty block. Otherwise, the language would need some form of a "noop" method to use when you really do not want anything done.

I personally would prefer the explicit "noop" statement -- but that is not how Java is defined.

怎樣才叫好 2025-02-04 20:44:48

为什么?这是因为编译器作家更容易。 if if if code>之后,您无需提出特殊情况

if (cond && maybeFunc())
    ;// Code here I want to ignore

如果(cond),即使允许它实际上是一个可怕的想法, 。允许更容易,然后添加一个案例检查此问题。

Why? It's because its easier for compiler writers. You don't have to make a special case to check for semicolons after if(cond) and has an added usage of allowing

if (cond && maybeFunc())
    ;// Code here I want to ignore

Even though it's actually a terrible idea to allow this. It's just easier to allow and then to add a case to check this.

晨敛清荷 2025-02-04 20:44:48

Just a FYI about the usability and what difference it makes or can make if there is a statement like that

Consider a piece of code like the following.

int a = 10;
if ((a = 50) == 50);

System.out.println("Value of a = " + a);

显然,在这种情况下,如果语句确实会更改输出,则。因此,这样的陈述可以有所作为。

在这种情况下,这可能有用或更好地说对程序产生影响。

Just a FYI about the usability and what difference it makes or can make if there is a statement like that

Consider a piece of code like the following.

int a = 10;
if ((a = 50) == 50);

System.out.println("Value of a = " + a);

Clearly in this case, the if statement does change the output. So a statement like that can make a difference.

This is a situation where this could be useful or better to say have an impact on program.

提赋 2025-02-04 20:44:48

A few definitions from the jls explain this (chapter 14):

Blocks are Statements

As stated

The if-clause

Though this is as well the case for for and while-loops, I'll use if-statements.这些规则几乎相同。 if-statement 的句法描述可以找到在这里

IfThenStatement:
    if ( Expression ) Statement

IfThenElseStatement:
    if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf:
    if ( Expression ) StatementNoShortIf else StatementNoShortIf

因此我们可以在这里使用块。

但是为什么它与之搭配; ?

; 定义为 emptystatement link ),也是 statement noshortif 。因此,在有条件的代码中,例如 If-Statement 和loops,我们可以用 block emptyStatement ,如果 statement> statement> statement nonoshortif 语句是必需的。

因此,如果(表达式)emptyStatement 有效。

为什么这不给错误?

非常简单:如果Java找到无效的语法,Java会出现错误。但是如果(表达式)emptyStatement 是完全有效的语法。而是 javac 使用适当的参数启动,会发出警告。 为此,列出了警告名称。因此,使用 -Xlint进行汇编:ALL -Xlint:empty 将对此发出警告。

您的IDE也应该可以选择此类警告。
对于Eclipse,请参见@nullptr的答案。 In IntelliJ, you can press Ctrl + Shift + A, enter empty body into the search field and enable the warning (marked in the image)

What is this even used for?

老实说,从简约的角度来看,它没有太多用途。通常有一种方法可以在没有“无所事事”命令的情况下完成工作。 It's rather a question of personal preferences, whether you rather use

if( a() && b() );

or

if( a() ) b();

and same would apply to other cases, in which the EmptyStatement is used.在此主题上要考虑的一个重要点是代码的可读性。在某些情况下,使用NO-OP可以更易读代码。另一方面,在某些情况下,使用 emptyStatement - 上面的示例将计算到以后的IMO中,代码变得更加困难。

A few definitions from the jls explain this (chapter 14):

Blocks are Statements

As stated here, a Block is a StatementWithoutTrailingSubstatement, which in turn is a StatementNoShortIf, which is a Statement. Thus where ever any of these is required, we can insert a Block.

The if-clause

Though this is as well the case for for and while-loops, I'll use if-statements. These rules are pretty much the same. The syntactical description of if-statements can be found here.

IfThenStatement:
    if ( Expression ) Statement

IfThenElseStatement:
    if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf:
    if ( Expression ) StatementNoShortIf else StatementNoShortIf

So we can use our block here.

But why does it work with ; ?

; is defined as the EmptyStatement (link), which is as well a StatementNoShortIf. So in conditional pieces of code, like if-statement and loops, we can replace a Block with a EmptyStatement, if a StatementNoShortIf or Statement is required.

Thus if(Expression)EmptyStatement works.

Why doesn't this give an error?

Pretty simple: java gives an error if it finds invalid syntax. But if(Expression)EmptyStatement is perfectly valid syntax. Instead javac gives a warning if launched with the proper parameters. The full list of warnings that can be dis-/enabled lists the warning-name empty for this purpose. So compilation with -Xlint:all or -Xlint:empty will generate a warning about this.

Your IDE should have an option to enable this kind of warning as well.
For eclipse, see @nullptr's answer. In IntelliJ, you can press Ctrl + Shift + A, enter empty body into the search field and enable the warning (marked in the image)

IntelliJ enable empty-body warning

What is this even used for?

To be honest, there's not much use in it from a minimalistic point of view. There's usually a way to get things done without a "do nothing" command. It's rather a question of personal preferences, whether you rather use

if( a() && b() );

or

if( a() ) b();

and same would apply to other cases, in which the EmptyStatement is used. An important point to consider on this topic is readability of code. There are occasions, where code becomes more readable by using the no-op. On the other hand there are cases, where code becomes quite a lot harder to comprehend with using the EmptyStatement - the above example would count to the later IMO.

风为裳 2025-02-04 20:44:48
if(a==b)
    println("a equals b");

您可以使用IF语句,没有 {} 如果仅执行一行,则使用,如果(a == b); 您是在说如果他们说如果他们平等,执行和空语言...因此,它无能为力,然后在IF块之外返回您的普通循环。

if(a==b)
    println("a equals b");

You can use an IF statement without {} if there is only a single line to be executed, so by using if(a==b); you are saying if they equal, execute and empty statement... So it will do nothing, and then return to your normal loop, outside of the IF block.

简单爱 2025-02-04 20:44:48

我可以想到一个需要一个空语句的方案(不是条件,而是 while 循环)。

当程序只希望从用户进行明确确认时。当用户确认之后的工作取决于其他一些内容,并且用户希望控制何时继续进行时,可能需要这是必需的。

    System.out.println("Enter Y to proceed. Waiting...");
    System.out.println("");

    while(!(new Scanner(System.in).next().equalsIgnoreCase("Y")));

    System.out.println("Proceeding...");
    // do the work here

I can think of a scenario where an empty statement is required (not for if condition but for while loop).

When a program just want an explicit confirmation from the user to proceed. This may be required when the work after the user confirmation depends on some other things and user want to take control of when to proceed.

    System.out.println("Enter Y to proceed. Waiting...");
    System.out.println("");

    while(!(new Scanner(System.in).next().equalsIgnoreCase("Y")));

    System.out.println("Proceeding...");
    // do the work here
避讳 2025-02-04 20:44:48

看一下:

int a,b,c = 0;
if(a == b){
   c =1;
} 
System.out.print(c);//1

因此,您可以这样写:

if (a == b)c=1;

但是,如果此代码是这样的:

int a,b,c=0;
if (a != b){
}
if (a == b ){
  c =1;
}

您可以这样写:

if(a != b);
if(a == b )c=1;

因此,如果(a!= b); 注意,您会知道

look this:

int a,b,c = 0;
if(a == b){
   c =1;
} 
System.out.print(c);//1

so, you can write like this:

if (a == b)c=1;

but,if this code is this:

int a,b,c=0;
if (a != b){
}
if (a == b ){
  c =1;
}

you can write like this:

if(a != b);
if(a == b )c=1;

so,you will know if(a != b); do noting

奢欲 2025-02-04 20:44:48

IF中的半彩词指示终止IF条件(如Java ; 中)作为语句的结尾,因此在执行后的语句。

The semi-colon in the if indicates the termination of the if condition as in java ; is treated as the end of a statement, so the statement after if gets executed.

伪装你 2025-02-04 20:44:48

结束时的分号
if(a == b);只需单行完成语句,这意味着忽略条件的结果,然后继续从下一行执行
此代码很有用,另一方面,有时会在程序中介绍错误,例如

案例1.

a = 5;
B = 3;
如果(a == b);
prinf(“ A和B是平等的”);

案例2。

a = 5;
B = 5;
如果(a == b);
prinf(“ A和B是平等的”);
将在屏幕上打印相同的输出...

Semicolon at the end of,
if(a==b); simply finish the statement in single line which means ignore the result of condition and continue the execution from the next line
This code is useful, on the other hand sometime introduce bug in program, for example,

case 1.

a = 5;
b = 3;
if(a == b);
prinf("a and b are equal");

case 2.

a = 5;
b = 5;
if(a == b);
prinf("a and b are equal");
would print the same output on the screen...

白首有我共你 2025-02-04 20:44:48

在研究课堂的编程分配时,我正在与doodads的n网格一起工作,并将随机doodad的特征与上述,下方,左下和右上方的特征进行比较时,我发现了这一点,以防止嵌套语句和潜在边界异常。我的目标是最大程度地减少代码,并避免嵌套IF-Statement。

if (row == 0); 
else (method (grid[row][col], grid[row-1][col]));
if (row == N-1);
else (method (grid[row][col], grid[row+1][col]));
if (col == 0);
else (method (grid[row][col], grid[row][col-1]));
if (col == N-1);<br>
else (method (grid[row][col], grid[row][col+1]));

其中方法(doodad a,doodad b)在a和b之间进行一些操作。

另外,您可以使用异常处理来避免使用此语法,但是它对我的应用程序有效。

While working on a programming assignment for class where I am working with a N by N grid of doodads and comparing characteristics of a random doodad to those above, below, left, and right, I found a nice use of this to prevent nested statements and potential boundary exceptions. My goal was to minimize code and keep from nesting if-statements.

if (row == 0); 
else (method (grid[row][col], grid[row-1][col]));
if (row == N-1);
else (method (grid[row][col], grid[row+1][col]));
if (col == 0);
else (method (grid[row][col], grid[row][col-1]));
if (col == N-1);<br>
else (method (grid[row][col], grid[row][col+1]));

where method(Doodad a, Doodad b) does some operation between a and b.

Alternatively, you could use exception handling to avoid this syntax, but it works and works well for my application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文