These are valid lines of code, because there is no reason for them not to be - infact, it would require additional efforts to program them so that they were not, and the language developers did not want to carry out these additional efforts. (However, in some languages, such as C #, many of them are unacceptable).
C ++ defines a “statement” (something ending with a semicolon at the end of a line) to be several specific statements, such as return x or throw y , or any expression. Expressions must be allowed so that statements such as foo(); executed foo(); . Language developers could explicitly define an operator that includes function calls and a few other things, instead of just including an expression, but it was easier to just say an expression. This covers true; false; , 1+1; , 1==1; , 1; etc.
In the case of curly braces, it is actually permissible (and sometimes useful) to make curly braces that have no associated if / while / for / etc. They are announcing a new scale. For example, the following prints 2 :
void foo() { int x = 2; { int x = 3; } cout<<x<<endl;
A language specification can be expressed (approximately) as something called context-free grammar, and you can find more information about it on Wikipedia.
source share