It is not after the if
, the semicolon exists to close a statement, Like you always used in "end of line" (which in the background does not close the line, we say this informally, he close the command), including used in other points. If the command has nothing it encloses nothing, no problem at all (it is called Empty statement). If you do this, it’s the same:
int base_calc(int cb)
if( cb < 30 ) {
} else if( cb < 20 ) {
cb = 30 - 1;
} else {
cb = 30;
}
return cb;
}
It was used there to make it clearer in the code that the intention is to create a block that does nothing. Note that my code gives less idea that there is something empty to be done there, it is not the end of the world, but you waste a little time to make sure that is it.
You can rewrite the code so you don’t need this. In C or C++ this is not often done because it is less efficient, the rewriting is usually better (although it is not always necessary all this efficiency.
Just be careful with the keys there. This does not give the same result:
int base_calc(int cb)
if( cb < 30 )
;
else if( cb < 20 )
cb = 30 - 1;
else
cb = 30;
return cb;
}
I put in the Github for future reference.
In this case the ;
is closing the statement if
and nothing further will be executed. Luckily there is a else if
then it can’t be loose so it will give a syntax error in the compilation.
Command null, does nothing.
– anonimo
It may be a way of saying that the block is purposely empty (unlike a blank line that could suggest forgetfulness).
– Bacco
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero