3

I am trying to discern the logic behind punctuator usage in C++, particularly the semicolon. This is my progress so far, with some questions:

  • A declaration introduces a type, class or object into a scope, e.g. int i;
  • An expression is a sequence of operators and operands, e.g. a=i+1; i++;
  • A statement is an expression or a declaration.

  • () Parenthesis group parts of an expression and surround tests e.g. if(a==b), while(a==b), switch(myTestVal) and for(int i=0;i<5;i++)

  • {} Braces define scope and group statements and initialisation lists for arrays, enums and structs, but why NOT classes! In addition they are required in a switch statement to enclose its body so a break knows where to continue from.

  • , Commas separate items in a list, e.g. an argument list or array initialisation list.

  • : Colons are used after labels, such as after the case part of a switch statement and to separate parts of a statement, such as in the tertiary operator '?'.

    However ; rather than : is used to separate the parts of the for statement e.g. for(i=0;i<5;i++) — Why is this?

  • ; Semicolons terminate statements (expressions and declarations) except where they are terminated by ), or : e.g. in a test:(a==(c+b*d)) or argument list.

Note that } does not count as terminating a statement, so after the } at the end of a function or class declaration a ; must be used, since the entire declaration is a statement, made up of many other statements. However, a function or class implementation is not a declaration (since the function or class must already have been declared); therefore it does not count as a statement and so does not have a closing ; after the closing }

One last oddity: why is a ; required after a do...while?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Gutzy
  • 31
  • 2
  • 3
    Note that comma is also an operator. – Oscar Korz Oct 19 '11 at 17:09
  • 2
    Not every convention is always going to seem logical from a human point of view, since it must be possible for a compiler to do its work and a source file must be completely unambiguous. But I'd certainly like to hear it as well if someone knows interesting tidbits of history about these conventions. – G_H Oct 19 '11 at 17:12
  • Not as bad an Question as it is made up to be.Voting to reopen,Don't see whats not a real Q in this. – Alok Save Oct 19 '11 at 17:15
  • 2
    Semicolons are used in `for`, because it's three independent expressions (expression!=statement). `{}` declare _scopes_. If you have a switch in another switch, the `{}` clarify the scope of each one. Every statement ends in `;`. Just like `int var1, var2;` you can have `class A {} var1, var2;`, it's a declarative statement. – Mooing Duck Oct 19 '11 at 17:34
  • Related: http://stackoverflow.com/questions/942251/in-c-c-why-does-the-do-whileexpression-need-a-semi-colon – Flexo Oct 20 '11 at 13:40

3 Answers3

3

For the definitive answer as to how semi-colons are used, see:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

Refer to Appendix A.

As to why semi-colons are required where they seem to be redundant, e.g.

struct A {};

You can in fact say:

struct A {} a;

So there is a location between } and ; where you can optionally put an identifier. Hence the necessity for more complex syntax.

But not every inconsistency is justified based on a globally valid "logic". C++ inherits syntax from C, and both languages have an evolved syntax that had to introduce new capabilities without breaking large bases of existing code. They bear the scars of this evolution.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
3

Commas separate items in a list, e.g. an argument or initialisation list.
Yes, but note that commas can also be overloaded to do absurd things too. (Don't do that)

Colons are used to separate parts of a statement
Yes, Colons are used wherever a comma or semicolon wouldn't make sense. It's like an "other" token. Not really sure why for loop uses semicolons as a separator (those are expressions, not statements).

Braces group statements.
No, {} declare scopes. Anything inside {} cannot be seen by things outside of the {} without scoping, like std::string, or string a; a.begin(); (a is a string and thus can access members inside string's scope.) Scopes also clarify nested switches, and nested... anythings.

switch(a) {
case 1:
case 2:
    switch(b) {
    case 3:
    case 4:
    }
case 5:
}

Semicolons terminate statements
Every statement ends in ;, except for and while. The top of a for loop contains expressions, not statements. You can't put a while in the for declaration thingy. (what is that called?) Yeah. It's very confusing.

However, a function or class implementation, is not a declaration
Yes and No. A function is a definition, and not a statement. It requires no semicolon. A class definition is technically part of a variable declaration statement, so it does require a semicolon.

int a;  //declare a as an int
class A {} a; //declare a as an A class I just made
class {} a,b,c;  //declare a,b,c as unnamed class types
int;  //int exists
class A {};  //A exists
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

For the switch statement, if there were no braces, how would you know the last case was over and it was time to "continue after the switch"? The break statement transfers control to the first line of code after the switch. The braces are the only way you can know where that is.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85