Semicolon at end of 'if' statement

前端 未结 18 1626
没有蜡笔的小新
没有蜡笔的小新 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:27

    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) {
        ;
    }
    
    0 讨论(0)
  • 2020-11-22 01:28

    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.

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

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

    Java->Compiler->Errors/Warnings

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

    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.

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

    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

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

    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.

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