The meaning of ;

This is a discussion on The meaning of ; within the C++ Programming forums, part of the General Programming Boards category; Ok so everything I have read has defined a semicolon as just an ending statement. But it's obvious that it ...

The while is setup so that you provide a conditional, and then you provide a single statement that includes what you want to do. The syntax of the while requires that the condition go inside parentheses, and that the statement comes after that. Since the semi-colon indicates the end of a statement, your first piece of code does nothing in this "what you want to do" part.

In most cases, you want to have more than a single statement be repeated by the loop. In that case, you use a compound statement, or a block. The braces indicate the block. A single block counts as the single statement in the "what you want to do" section of the while. So this:

Code:

while (condition);

is the same as this:

Code:

while (condition)
{
}

The mistake people make is they do this:

Code:

while (condition);
{
// more statements
}

The problem there is that the semi-colon ends the single statement that you are allowed to have with the while, and the block that comes after it is not included in the loop.

So in the end, the semi-colon is still just an indicator of the end of a statement. The reason you don't put it after a while is because the condition part doesn't require a semi-colon to indicate where it ends, and if you put one there, it will indicate the end of the "to do" part rather than the condition.

You can also see the problem as following: for, if, while and do...while statements don't absolutely need brackets, if there is only one line of code in the loop. Therefore, you can do the following:

Code:

int i = 10;
while(i >= 1)
std::cout << i;

However, if you put a semi-colon right after the while statement, the compiler understands that you need only one line of code and when it searches for the action to accomplish, it finds nothing, therefore it does nothing.

You can also see the problem as following: for, if, while and do...while statements don't absolutely need brackets, if there is only one line of code in the loop. Therefore, you can do the following:

Code:

int i = 10;
while(i >= 1)
std::cout << i;

However, if you put a semi-colon right after the while statement, the compiler understands that you need only one line of code and when it searches for the action to accomplish, it finds nothing, therefore it does nothing.

However, in most business environments, alot of standards will still require you to put the brackets in anyway, just to add consistency and readability. Its easier to identify if, when and for statements when you see those brackets, so its easier for debugging. Also, sometimes when people are adding stuff to code, they may just add a line after the while statement, and forget to put in the brackets, this causes problems because only the first line will be run through the while statement, and any following lines will run always, despite the success or failure of the while statement. (Also, it will not run multiple times if your looping, because its considered to be outside the 'scope' of your while statement).

Ok so everything I have read has defined a semicolon as just an ending statement. But it's obvious that it has more power to it then just that.

Could somebody give an simple explaination on what exactly it means and why I am putting it after everything?

The semicolon is a terminator which sets different elements apart from each other. It tells the compiler "This structure definition/variable declaration/statement is finished." It exists mostly to make the compiler's job easier. Without it, certain parts of the C and C++ languages would be ambiguous (kind of how human languages can be ambiguous), and you can't have that in a programming language, so... semicolons it is.

This leaves you with questions like, "Why do I put a semicolon after a struct/class definition, but not after the closing brace of a function definition?" The answer, which is not really satisfying, is "Because the language doesn't require a semicolon there to be unambiguous." Remember that the compiler needs to know when something is "finished." In the case of a function, the compiler knows it's done when it sees the final closing brace. Therefore, no semicolon is required there. So you are left basically memorizing where semicolons go and where they don't.

With regard to your specific example, the grammar of a while-loop is basically "The word 'while' followed by an expression in parentheses, followed by a statement or compound statement." If you place a semicolon after the while-loop, the compiler believes it is a statement (that happens to be completely empty). This is hardly ever what you want.

Now think about a language like Pascal, where semicolons are not terminators, but separators. This leads to even more confusion, because, for example, you DON'T have to put a semicolon after the last statement in a block (since there is no following statement it must be separated from), while in C you do.

Your question is a good one that can't really be answered fully without getting deep into the grammatical details of the language.

Awesome, thank you all. That helps quite a lot. You guys certainly make learning this stuff easier. I had a chance to own a really good C++ book but I waiting a bit too long on it and ended up having to get C++ for Dummies, which despite it's very poor structure and lack of definitions when you need then. It's teaching me well enough I guess, I just need alittle extra help here and there.