-4

Instead of writing

while()
{
 lbl:
}

You have to write

while()
{
 lbl:;
}

Why? It doesn't make any sort of sense to me. This is the case even if there was actual code in the loop

melpomene
  • 84,125
  • 8
  • 85
  • 148
Toby Lam
  • 5
  • 1
  • 4
    FYI, this has nothing to do with the `while`. For example: `{label:;}`. – Cornstalks Jul 15 '18 at 12:45
  • 3
    Essentially: because a label is an optional part of a statement, and the statement needs to be a complete statement. A label followed by `:` on its own is not a complete statement. `;` is an indicator of the end of a statement. The rule helps the compiler parse sensibly, since it can unambiguously recognise the beginning and end of each individual statement (e.g. by presence of `;` or, in the case of block statements, the `{` and matching `}`). – Peter Jul 15 '18 at 13:00

1 Answers1

-1

If you code in C, because the C11 standard defines that. Check by reading n1570, §6.8.1. BTW, other standards of C have a similar rule.

If you code in C++, because the C++11 standard defines that. Check by reading n3337, §6.1. BTW, other standards of C++ have a similar rule.

Intuitively, a label concerns a statement, and the closing brace is not a statement (but a block terminator). And a semi-colon (when used alone) is ending an empty (or null) statement.

If you are not happy with that rule, feel free to use some different programming language, or to define your own one (and implement it, perhaps by compiling it to C). Maybe you could look into Scheme (read SICP then R5RS) and its call/cc primitive (a different, and more powerful, control primitive than plain goto).

You could also lobby the C++ (or C) standard committee to change the rules. In practice, that requires a world-wide reputation and many years of full time work (otherwise, they won't consider your proposal seriously). Before writing proposals to change the C++ standard, be sure to learn it very well and to provide a sample implementation of your ideas.

Many C++ implementations are free software (such as GCC or Clang). You are allowed to study them and to improve them (at least in your fork; however be sure to comply with their open-source license). However, there are large pieces of code (many millions lines of source code), so you'll need years to understand them.

Remember that a programming language is a specification (not a software) which defines not only the syntax but also the semantics.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547