Thread: whats the difference between

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    whats the difference between

    why doesnt the compiler give an error when u have
    Code:
    else
    Code:
    else;
    whats the difference or there is none?
    When no one helps you out. Call google();

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the compiler recognizes else as a keyword and expects either an opening { followed by one or more statement and then a closing } or else it assumes that the next statement is a single line to be controlled by the else. A statement is a line of code ending with a semicolon.


    Therefore else by itself is incomplete and else followed immediately by a semicolon basically means do nothing.
    Code:
    if(true)
      doThis();
    else
    {
      doThat();//statement controlled by else
    }
     
    if(true)
      doThis();
    else
      doThat();//statement controlled by else
     
     
    if(true)
      doThis();
    else; //do nothing, since the statement controlled by else is empty

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    this should be perfectly legal

    Code:
    if(condition) {
          do something
    }
    else
         ;
    It sees the semicolon as a blank line, so it does nothing for the else.

    Kind of like
    Code:
    while(1)
         ;
    // or
    while(1);
    That does nothing forever.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it is, but it's a completely different concept... that has to do with the way statements are structured, not the "empty else" as my programming teacher used to call it
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 6
    Last Post: 08-26-2006, 11:09 PM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM