cases or ifs?

This is a discussion on cases or ifs? within the C++ Programming forums, part of the General Programming Boards category; which one is better?...

  1. #1
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859

    cases or ifs?

    which one is better?
    Yoshi

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Depends on what you're trying to do.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Neither is better all the time. Pick the one that suits your code and needs at the time. Don't be afraid to experiment and find out for yourself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    A Banana Yoshi's Avatar
    Join Date
    Oct 2001
    Posts
    859
    ...checking for getch()?
    Code:
    a = getch();
    
    if (a == 40)
       do something
    
    else if...
    Yoshi

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    getch();

    One of my favorites...until <conio.h> went down the dilly with the onset of the new Standard.

    Scrap it. Besides, you're only going to get one character. And, it isn't portable. Unix systems, for one, don't even handle it.

    Your code is going to go down the dilly, as well. Nifty for one 'char' entries, but don't expect it to work for your example. You'll catch the '4', but...the '0' will be left in the stream and you'll have to deal with that.

    Hammer, as usual, is correct. (Not being a pain, but they're "switch" statements. "Cases" are the elements of a "switch" statement.)

    Personally, I hate extended IF's and IF...ELSE code. Switch statements seem so much cleaner. On the flip side, they don't always do the job since the case elements only take "integral" values. Again, as Hammer suggests, pick one that suits your needs.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Unix systems, for one, don't even handle it.
    Some do under the Curses libraries or there's a custom version that vVv supplied on the FAQ board. But this is going OT, so I'll stop there
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    And, I'll stand corrected.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >> And, I'll stand corrected.

    Well, yes and no. It is still non-standard. Therefore you should try and not use it.

  9. #9
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    So what is the standard function that does the same as getch()?
    Somebody told me once that there is getchar() is that the one?
    none...

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ammar
    So what is the standard function that does the same as getch()?
    Somebody told me once that there is getchar() is that the one?
    There isn't one. getchar() is similar, but not at all the same.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Hammer
    There isn't one. getchar() is similar, but not at all the same.
    Thanks alot, I'll try searching the web for getchar() to know what's the difference, between it and getch().
    none...

  12. #12
    Registered User adamviper's Avatar
    Join Date
    Nov 2002
    Posts
    132
    i like cases (yes i know but this one liner should be accepted).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 08:42 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. I need to use 0 to quit a program
    By cwest in forum C Programming
    Replies: 2
    Last Post: 12-15-2001, 08:37 AM
  5. Please help me with cases!!!
    By halifax in forum C Programming
    Replies: 1
    Last Post: 10-14-2001, 02:29 PM

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