if statement

This is a discussion on if statement within the C Programming forums, part of the General Programming Boards category; i tryed to use this if statement and there is an error. This is what it says: error C2181: illegal ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    if statement

    i tryed to use this if statement and there is an error. This is what it says:
    error C2181: illegal else without matching if

    and this is the 'else' that it points to.
    if(year>1970)
    yearDif=year-1970;
    printf("Bigger,\n");
    else
    yearDif=1970-year;
    printf("Smaller,\n");
    printf("The difference in years is %i\n",yearDif);

    what am I doing wrong?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Code:
    if (year > 1970)
    {
    	yearDif = year - 1970;
    	printf("Bigger,\n");
    }
    else 
    {
    	yearDif = 1970 - year;
    	printf("Smaller,\n");
    }
    printf("The difference in years is %i\n",yearDif);
    I hope you indented that code in your editor too.
    Use code tags to preserve indentation.
    Use brackets when the ifs are two or more lines.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Use brackets when the ifs are two or more lines.
    Or use curly brackets in ALL if-statements. That avoids "funny" effects when you have:
    Code:
    if (something) 
       n++;
    and you decide to add a printf to the if branch it so that you can debug:
    Code:
    if (something)
       printf("Hit something");
       n++;
    Now n++ is done whether something is true or not, because there are no braces around the printf/n++ statements.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    I don't like brackets in single-line ifs. Just remember the rule that two or more lines require brackets and you'll be fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I don't like brackets in single-line ifs. Just remember the rule that two or more lines require brackets and you'll be fine.
    And I guess you've never quickly added a line inside an if, to see if gets hit or some such, and then spent several minutes (or worse) on trying to figure out why it all of a sudden behaves completely different?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    As far as I can remember, if I added a new line to the if, I always add brackets, so it's never really been a problem to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    As far as I can remember, if I added a new line to the if, I always add brackets, so it's never really been a problem to me.
    What if you're working on a team and someone who's not so "careful" edits your code and doesn't add the braces? Just remember, you can pick your friends, and you can pick your nose, but you can't always pick the other developers on your team.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by cpjust View Post
    What if you're working on a team and someone who's not so "careful" edits your code and doesn't add the braces?
    Then I'd say that one is at fault!
    OK, but seriously, if I work alone, then I don't do it. If I don't work alone and I'm not required to do it, I don't. If I don't work alone and I have to do it, I do it. Otherwise not.
    This problem can be figured out with a simple debug anyway!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by cpjust View Post
    What if you're working on a team and someone who's not so "careful" edits your code and doesn't add the braces? Just remember, you can pick your friends, and you can pick your nose, but you can't always pick the other developers on your team.
    What if Venus is in alignment with the Moon and Earth and the North Polar Star? Image that. Wouldn't that be a sign to be concern about?
    I suppose it can't be helped to have people trying to beat the dead horse all the time.
    Last edited by Aia; 01-25-2008 at 06:43 PM.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, let me put it another way.
    Without the braces, the only thing you have to prove that you don't have a bug is the indentation. If somebody (maybe even yourself) comes along later and tries to reformat the code, and accidentally messes up the indentation, now you've got nothing, plus it's harder to read. If the braces were there, even without the indentation, you'd know exactly what the code was intended to do.

    I recently fixed some bugs where somebody tried to cram two lines into an if statement without braces. Were they brain-dead morons? Probably. But if they would have gotten into the habit of always using braces, that wouldn't have happened.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by cpjust View Post
    I recently fixed some bugs where somebody tried to cram two lines into an if statement without braces.
    lol. Job security para ti!

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    I have to admit I do the same as Elysia in this respects.
    I've never run into the problem of missing curly braces because the first thing I automatically do when adding something additional to an if-statement is make sure it has brackets.
    It is similiar to how you would enhance this:
    Code:
    if (a || b)
    to this:
    Code:
    if ((a || b) && c)
    You know you need to add extra brackets when you make such a change, but nobody expects you to write this initially:
    Code:
    if ((a || b))
    True there may be people out there that don't automatically check for such things, and regularly cause such a problem. I'd say that they likely have a tendancy not to learn from their own mistakes, and likely wont get far as a programmer. You get to work with all sorts of code styles in your life. Not everyone is going to write perfect code for you.

    Dare I say YAGNI applies?

    Well that's my 0.02c anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yagni ??

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    A mythical monster that eats programmers for breakfast. Or You Ain't Gonna Need It.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by cpjust View Post
    OK, let me put it another way.
    Without the braces, the only thing you have to prove that you don't have a bug is the indentation. If somebody (maybe even yourself) comes along later and tries to reformat the code, and accidentally messes up the indentation, now you've got nothing, plus it's harder to read. If the braces were there, even without the indentation, you'd know exactly what the code was intended to do.

    I recently fixed some bugs where somebody tried to cram two lines into an if statement without braces. Were they brain-dead morons? Probably. But if they would have gotten into the habit of always using braces, that wouldn't have happened.
    Maybe I should add another point then:
    Efficiency. Do you know that you tend to mess up if-statements by adding two or more lines to an if without adding braces? If yes, you can initially place those brackets there. If not, then you don't have to.
    To me, it seems like forcing someone to use another coding style as opposed to what someone usually uses. It's doable, but it doesn't mean you like it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 01:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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