Thread: semi-colon - where is it needed

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    semi-colon - where is it needed

    Why isn't the semi-colon (;) req'd after this statement
    Code:
    void addIncrement() { count += increment; }
    in the following code? Is it because it is an automatically inlined function or just because it is a function or for some other reason? Please give reason why ; is not needed or required here. It is a statement isn't it. A semi-colon is required at the end of a statement.
    Code:
    // Fig. 7.2: fig07_02.cpp
    // Using a member initializer to initialize a
    // constant of a built-in data type.
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class Increment {
    public:
       Increment( int c = 0, int i = 1 );
       void addIncrement() { count += increment; }  //Why isn't a semi-colon (;) req'd here?
       void print() const;
    
    private:
       int count;
       const int increment;		// const data member
    };

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    There is no semicolon at the end of a function definition... Function prototypes & function calls require 'em. I don't know why! You just have to learn this stuff, or look it up. There's no semicolon at the end of a for statement either... I don't know why.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619

    Re: semi-colon - where is it needed

    Originally posted by kes103
    It is a statement isn't it. A semi-colon is required at the end of a statement.
    No, it is not a statement. The statement is "count += increment;", and that DOES end in a semicolon.

    Statements are not in any way related to line breaks. For example:

    Code:
         int
         i
         =
         0
         ;
    is fully legal, and is one statement. This:

    Code:
    int i = 0; int j = i; j++; i--; j+=i;
    is five statements.

    Things like functions definitions and loops require no such semicolons. Because whitespace is irrelevant (except within tokens), your code is exactly equivalent to:

    Code:
    void addIncrement() 
    {
         count += increment;
    }

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why does a for and while loop not need a semicolon, but a do while loop does?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Could we simplify by saying that it is the curly braces
    Code:
    {}
    that void the requirement for the semi-colon(;)? That is, with the exception of structure (do/while included?) and class declarations and array initialization lists, the semi-colon is illegal or at least not required after the curly braces
    Code:
    {}
    ?
    Possibly the do/while does require the ";" because it doesn't end with a curly brace "}". The braces enclose the body of somthing. As I understand it, a very short function definition without a body enclosed in braces, requires the semi-colon(;). That is the observation I have made at least. Note: by why, I mean what "syntax rule" allows or disallows it.
    Last edited by kes103; 09-11-2003 at 05:32 AM.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    a rule isnt a rule if there are more exceptions than not. saying that curly braces negate the need for semicolons is just plain wrong, but with a little familiarity with the language proper use becomes common knowlege, and thats the only rule you can accurately use. whenever you learned keywords you didnt have a 'rule' that you followed. you just had to know them. same way with semicolon usage.

    As I understand it, a very short function definition without a body enclosed in braces, requires the semi-colon.
    it isnt the function that needs the semicolon, but the statement in the function. an if statement with only one line and no braces is the same thing

    if (this)
    do this;

    its not the if statement its the statement within the if that needs the semicolon
    Last edited by ...; 09-11-2003 at 07:53 AM.
    I came up with a cool phrase to put down here, but i forgot it...

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    161
    Curly braces do void the rule for a semi-colon because blocks themselves are considered statements.

    Statements in C++ are one of the following:

    Code:
    1. for(...) statement
    2. while(...) statement
    3. do statement while(...) ;
    4. return [expr] ;
    5. expr ;
    6. ;
    7. { [statements] }
    There are also the exception handling statements: try.. catch and throw.

    The semi-colon typically follows an expression to turn that expression into a statement. Being that blocks are inside curly braces and group statements together, the semi-colon would be redundant.

    I'm not sure why class definitions require semi-colons or why function defintions can't use a single statement. Maybe someone that is more familiar with the language design can help us out?

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I'm not sure why class definitions require semi-colons
    Class and struct definitions are as they are because one is allowed to instantiate classes/structs on the spot. E.g. this is legal:

    Code:
    int main(){
        struct {int x, y;} myPoint;
        myPoint.x = 5;
        myPoint.y = myPoint.x + 3;
    }

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    161
    Thanks, Cat. I had forgotten about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 10:44 AM
  2. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  3. comma and semi colon
    By studentc in forum C Programming
    Replies: 13
    Last Post: 05-14-2004, 05:49 PM
  4. another question on using The colon :
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 08-11-2003, 11:22 AM
  5. single colon
    By kes103 in forum C++ Programming
    Replies: 11
    Last Post: 07-17-2003, 04:21 PM