case/if

This is a discussion on case/if within the C++ Programming forums, part of the General Programming Boards category; I have noticed that case and if seem to be very similar in c++. I was wondering what the advantages ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Question case/if

    I have noticed that case and if seem to be very similar in c++. I was wondering what the advantages each has over the other. Thanks.
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  2. #2
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    you mean switch and ifelse?

    switches are much slower (not noticeably when working with, say, hello world) but a bit easier to read. if-else runs like a burning rocket and is used for asm/microprocessors. Even hello world might come out slow on a 2Mhz processor.
    Ph33r the sphericalCUBE

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    A case clause is used in conjunction with a switch statement. One disadvantage of a switch statement compared to if/else statements, is that it only works with ordinal types (int, char).

    Most times a switch statement will make your code more readable and well structured.

    If usually only use switch statements with constants, enums, etc. eg.
    Code:
    switch( day )
    {
         case MONDAY: 
         case TUESDAY:
         case WEDNESDAY:
         case THURSDAY:
         case FRIDAY:
                 printf( "Working day" );
                 break;
         case SATURDAY:
         case SUNDAY:
                 printf( "No work! Cool" );
                 break;
         default:
                 printf( "I dunno!" );
    }
    The code above is much simpler than it's if/else equivalent.
    Code:
    if( day == MONDAY || day == TUESDAY || day == WEDNESDAY
     || day == THURSDAY || day == FRIDAY )
        printf( "Working day" );
    else if( day == SATURDAY || day == SUNDAY )
        printf( "No work! Cool" );
    else
        printf( "I dunno" );

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    27
    thanks very much for the help
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >switches are much slower
    A switch statement is comparable in speed to a series of if..else if...else statements, to the point where the difference is negligable. If your compiler performs otherwise then it is hopelessly broken.
    My best code is written with the delete key.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Isn't a switch quicker than if..then..else if...else because it generates a jump table?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    its compiler specific
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  8. #8
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Concerning the limit on types, I don't see why you can't allow other types and simply have the compiler convert it to ifelse blocks during the early phases of compilation.

Popular pages Recent additions subscribe to a feed

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