Is there a rule regarding which statements don't need to be terminated with a semicolon?
Yes, it's covered in section 6, "Statement" of the C++ standard (section 6 of C++03, it may have changed in C++11 but I don't have access to that one at the moment).
There are a large number of statement types and not all of them need to be terminated. For example, the following if
is a selection statement:
if (i == 1) {
doSomething();
}
and there is no requirement to terminate that with a semi-colon.
Of the different statements covered, the requirements are:
Statement type Termination required?
============== =====================
labelled statement N (a)
expression Y
compound statements N (a)
selection statements N (a)
iteration statements N (a) (b)
jump statements Y
declaration statement Y
(a) Although it may sometimes appear that these are terminated with a semi-colon, that's not the case. The statement:
if (i == 1) doSomething();
has the semi-colon terminating the inner expression statement, not the compound statement, somthing that should be obvious when you examine the first code segment above that has it inside {}
braces.
(b) do
requires the semi-colon after the while
expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With