Semicolon at end of 'if' statement

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

提交回复
热议问题