Semicolon at end of 'if' statement

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

提交回复
热议问题