Semicolon at end of 'if' statement

前端 未结 18 1624
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 01:01

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          


        
相关标签:
18条回答
  • 2020-11-22 01:41

    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.

    0 讨论(0)
  • 2020-11-22 01:42

    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.

    0 讨论(0)
  • 2020-11-22 01:42
    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.

    0 讨论(0)
  • 2020-11-22 01:43

    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.

    0 讨论(0)
  • 2020-11-22 01:44

    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...

    0 讨论(0)
  • 2020-11-22 01:45

    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.

    0 讨论(0)
提交回复
热议问题