#1
  1. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    12

    loop terminations


    Hey!

    Can any1 settle a discussion for me?

    Myself and a colleague are *ahem* "discussing" why some loops in C finish with a semi-colon but others do not. eg if and do/while. Can anyone settle this for us. I do remember reading an explanation somewhere before but i cant remember what it was!

    Thanks :)
    -----------------------------------------------------------
    4dr14n
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    It's a matter of the compiler knowing where statements end, really. With most statements, you'll need the semi-colon to mark the end. When you're using a compound statement (using curly braces, '{' and '}'), it's pretty much clear where the statement ends, isn't it?

    Code:
    if (true) {
        /* do something */
    }
    That also explains why the do .. while loop construct -does- need a semi-colon: the curly brace isn't the last token in the statement.

    Code:
    do {
       /* something */
    } while (this is true);
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. Huh?
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    Scotland, UK
    Posts
    223
    Rep Power
    12
    Thanks for that! :)

    I thought it was something along those lines but couldnt quite explain what i meant.

    Nice examples!

    Thanks for your time!
    :)
    -----------------------------------------------------------
    4dr14n

IMN logo majestic logo threadwatch logo seochat tools logo