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 ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    15

    The meaning of ;

    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?

    Just a few days ago someone told not to use a : after a while conditional, they said that:

    Code:
    while (conditional);
    was equavilent to:

    Code:
    while (conditional)
    {
              ;
    }

    Now while I partially understood this I also didn't understand it at all. Could someone elucidate on that? Thank you before-hand.
    Last edited by HunterCS; 07-02-2007 at 04:08 PM. Reason: adding subcription

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    A semicolon is just saying "OK, you can go to the next line of code now" to the compiler, otherwise it gets confused. You can also write the loop like this:
    Code:
    while (conditional) { ; }
    That's how I think of it. You just space out all the other things to make it easy to read.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The syntax of control statements, be they if, while, switch or whatever, is

    <keyword>(<condition-or-something>) <sub-statement>

    The sub-statement is part of the control statement. If there was a ; after the condition, the control statement would end there.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    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.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    6
    Quote Originally Posted by Desolation View Post
    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).

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,238
    Quote Originally Posted by HunterCS View Post
    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.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    15
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the meaning of " >> "
    By arian in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2005, 09:40 AM
  2. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM
  3. The meaning of "Duh"
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-26-2003, 12:46 PM
  4. would you help me with Linked list, please?
    By unhwan in forum C Programming
    Replies: 1
    Last Post: 06-11-2002, 12:24 AM
  5. WINAPI: Meaning of HDC ?
    By Mecnels in forum Windows Programming
    Replies: 1
    Last Post: 01-21-2002, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21