switches and breaks

This is a discussion on switches and breaks within the C Programming forums, part of the General Programming Boards category; What are switches and breaks good for in C language? I know the moderators will get on my case for ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question switches and breaks

    What are switches and breaks good for in C language?

    I know the moderators will get on my case for not looking it up in a book or something, but my book is in the other room and I don't feel like going all the way there to look those two things up. Besides, you guys can probably explain it better than a book can.

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Yeah we probably can but we don't feel like sitting here and typing it all out just so you can be enlightened as to those two things

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    Trust me, you'll need to have exercise sometime.

    Anyways...

    Switches are good for when you need a functional approach to a multiple-choice situation. For example, if I were to read in an integer value from a user, and want to do different things dependant on its value, I could do:-
    Code:
    if (integer == 0)
       ...
    else if (integer == 1)
       ...
    else if (integer == 2)
       ...
    else if (integer == 3)
       ...
    However, this is not particularly easy to read, and you may end up repeating some sections completely. To this end, you can use switch to achieve cleaner-looking code:-
    Code:
    switch (integer)
    {
       case 0:
       {
          ...
          break;
       }
       case 1:
       {
          ...
          break;
       }
       case 2:
       {
          ...
          break;
       }
       case 3:
       {
          ...
          break;
       }
       default:
       {
          ...
          break;
       }
    }
    This way, it's easier to read what's going on. break is used here to prevent the code in the next section from being run (Omitting it would allow you to create sections that all choices go through, etc.) break is also used to instantly jump out of loops.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    switch( volk )
    {
        case LazyBastard:
            printf("I'm a lazy bastard.");
            printf(" Rather than actually getting");
            printf(" off my ass, I'll post to the");
            printf(" C board and maybe they");
            printf(" take pitty on me.\n");
            break;
    
        case SomewhatMotivated:
            printf("I'm somewhat motivated.");
            printf(" I'll see what I can find first");
            printf(", and if I can't find anything,");
            printf(" I'll post on the C board.\n");
            break;
    
        case WantToLearn:
            printf("I want to learn!");
            printf(" I've studdied my book,");
            printf(" and I have done my best");
            printf(" but I'm still stuck. I've");
            printf(" read the FAQ and the");
            printf(" sticky notes on the C");
            printf(" board. Having done all");
            printf(" of that, I'll post my code");
            printf(", a description of the issue");
            printf(", what I want it to do,");
            printf(" what it is doing, and any");
            printf(" error messages if I am");
            printf(" getting any.\n");
            break;
    }
    That's what switch is good for...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i know cases are good for making choice with numbers like:

    case 1:

    but could you use one with a string like:

    case "one":

    ive been trying to get something like that working since i read that case preform better than ifs and a page of ifs doesnt seem right.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but could you use one with a string
    No. case are for integral types only.

    To compare the strings, use strcmp() and a bunch of if statements.
    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
    Nov 2002
    Posts
    491
    case keyword can only accept single integer values. So you could do:

    Code:
    char ch;
    
    switch(ch)
    {
          case 'A':
              /* do stuff */
           break;
    ...

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    is there any alternatives for my function

    Code:
    int char2ascii(int data) {
        if(data == "<P>")
            data = '\0';
        else if(data == "&#32&#32&#32&#32&#32")
            data = '\t';
        else if(data == "&#32")
            data = ' ';
        else if(data == "&lt;")
            data '<';
        else if(data == "&gt;")
            data '<';
            
        return data;
    }
    using all those ifs just doesnt seem right to me.

  9. #9
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by quzah
    Code:
    switch( volk )
    {
        case LazyBastard:
            printf("I'm a lazy bastard.");
            printf(" Rather than actually getting");
            printf(" off my ass, I'll post to the");
            printf(" C board and maybe they");
            printf(" take pitty on me.\n");
            break;
    
        case SomewhatMotivated:
            printf("I'm somewhat motivated.");
            printf(" I'll see what I can find first");
            printf(", and if I can't find anything,");
            printf(" I'll post on the C board.\n");
            break;
    
        case WantToLearn:
            printf("I want to learn!");
            printf(" I've studdied my book,");
            printf(" and I have done my best");
            printf(" but I'm still stuck. I've");
            printf(" read the FAQ and the");
            printf(" sticky notes on the C");
            printf(" board. Having done all");
            printf(" of that, I'll post my code");
            printf(", a description of the issue");
            printf(", what I want it to do,");
            printf(" what it is doing, and any");
            printf(" error messages if I am");
            printf(" getting any.\n");
            break;
    }
    That's what switch is good for...

    Quzah.




    After reading Hammer's message posting, I interpreted that you cannot use case LazyBastard - only case 1, case 2, etc. Did I misinterpret?

    Also, what's the point of placing that third break for WantToLearn?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by volk
    After reading Hammer's message posting, I interpreted that you cannot use case LazyBastard - only case 1, case 2, etc. Did I misinterpret?

    Also, what's the point of placing that third break for WantToLearn?
    enum { LazyBastard, SomewhatMotivated, WantToLearn };

    The final break is optional. I prefer it for clarity's sake. Clean code is maintainable code.

    If you'd have read your book, you'd know that case statements require an integral value. That is to say, any non-decimal number who's range is the valid range of an integer.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by quzah
    enum { LazyBastard, SomewhatMotivated, WantToLearn };

    The final break is optional. I prefer it for clarity's sake. Clean code is maintainable code.

    If you'd have read your book, you'd know that case statements require an integral value. That is to say, any non-decimal number who's range is the valid range of an integer.

    Quzah.


    What is "enum"? LOL

    Ok, ok, I'll stop. I'll go read my book now.

    Thank you all, by the way.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by mart_man00

    if(data == "<P>")

    using all those ifs just doesnt seem right to me.
    1) This is not C++. You cannot use the == sign to accurately compare strings. (You could compare pointers that way, but not strings.

    2) There isn't really a much better way to do this. You could cycle through an array, that'd work. There are ways to do it, it's just use whatever you're comfortable with.

    Quzah.
    Hope is the first step on the road to disappointment.

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