More places for the semi-colen!

This is a discussion on More places for the semi-colen! within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I realized that you can place a semi-colen after brackets. Code: struct my_struct { }; int main( void ) { ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Lightbulb More places for the semi-colen!

    I realized that you can place a semi-colen after brackets.

    Code:
    struct my_struct
    {
    };
     
    int main( void )
    {
      while( void )
      {
    	switch( void )
    	{
    	};
      };   
    };
    And it has become a habit to put one after each function and loop and switch call. What about you guys?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    No. Why would you want people that use proper syntax to wonder what statement is ending when they see a random, unnessasary semicolon?
    Sent from my iPadŽ

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    What benefit do you see arising from this? I mean, why did you make it a habit?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Basically, since whitespace doesn't really matter, what you're doing is the same as
    Code:
    while(...) {
    }
    ;
    You just have the loop, and then you have an empty statement.

    Actually the fact that you have to place a semicolon after a class/struct definition is mostly annoying C legacy.
    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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    So you think its such a good idea CA? Lets take your semi-colon after brackets idea and test it out

    Code:
    int main()
    {
      if ( 1 == 0 )
      {
        cout<<"Blarg";
      };
      else
      {
        cout<<"Bleh";
      };
    };
    Yep that was a good idea.

    Oh you are saying just loops and functions? ok
    Code:
    int main()
    {
      bool startloop = true;
      int i = 0;
      if ( startloop )
        while (i < 10)
        {
          i++;
        };
      else
        cout<<"I'm not in the loop";
    };
    Moral of the story? Use them when you need them, don't just go sprinkling around your code.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Actually the fact that you have to place a semicolon after a class/struct definition is mostly annoying C legacy.

    the fact that you have to use them at all is annoying - omitting them wouldn't create any ambiguities for the parser that I can think of...so why should they be required?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Yes it would. A new line character in the syntax does not mean it's a new line of code. I think the parser would get confused as to what was a new line and what wasn't.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> A new line character in the syntax does not mean it's a new line of code.

    I'm not saying the newline should serve as a delimiter for statements. the parser already knows that expressions are linked together with operators. if it encounters an expression where an operator is expected, it would simply treat it as a new statement. for instance:

    Code:
    j = i = foo() n = bar()
    since 'n' is not an operator then it is the beginning of a new expression.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    But what about circumstances like this:
    Code:
    j = i = foo() ++x
    Is it foo()++ or ++x?
    Code:
    j = i = foo(); ++x;
    Ah, it is ++x.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Code:
    #include <stdio.h>
    
    typedef struct {
      int x;
    } foo;
    
    int main(void) {
      foo j, k;
    
      k.x = 5;
    
      {
        struct {
          int x;
        };       /* Removing this semicolon changes output. */
        k;
        j.x = k.x;
      }
    
      printf("%d\n", j.x);
      return 0;
    }

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    true, but I'd like to point out that both relied on the fact that the language allows statements having no effect. if that 'feature' of the language were removed (and I'd love to here an argument against that), then the intent would again be unambiguous. can you think of any examples excluding no-ops?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    There needs to be some sort of statemenet delineator, otherwise there would be way too many ambiguities for the parser (even if you disallowed statements that did nothing).

    For example:
    Code:
    int a = i; ++ p = &t;
    int a = i ++; p = &t;

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    JScript manages without compulsory semicolons (although to avoid programmer errors it gives an error if you have two statements on one line without a seperating semicolon). So, yes, with a few minor changes to the language you could probably do without them in C.

    EDIT: It seems that the application of ++ and -- in JScript, in the absence of semicolons, is somewhat complicated. You'd probably have to get rid of pre (or post) ++ and -- altogether.
    Code:
    // Post-increment on i
    i  ++ 
    
    j
    
    // Pre-increment on j
    i
    ++
    j
    Last edited by anonytmouse; 02-19-2006 at 12:23 AM.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ++p = &t;

    if p is a pointer then the increment would have no effect. on the other hand, if p was an object that returned something with prefix ++ that could be attached to t then it would have an effect. so I stand corrected (it was a nice daydream anyway ).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Here's a C snippet that changes meaning:
    Code:
    struct foo
    {
      int a, b;
    };
    
    bar;
    Here, bar is implicitely an int. If you omit the semicolon, bar is of type struct foo.
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 10:04 AM
  2. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 11:03 AM
  3. Replies: 1
    Last Post: 12-14-2002, 01:51 AM
  4. Replies: 10
    Last Post: 06-12-2002, 04:15 PM
  5. online gaming places
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-20-2002, 12:26 AM

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