Thread: Explanation of switch statements

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Explanation of switch statements

    Hello-

    I have to write a program that converts numbers into words. I know I have to use switch statements for this but I dont really understand the switch statement to begin with and how it workds and in turn I dont understand how I would use them for this program. Could someone help with an explanation? I dont want a program wirrten- just how switch statements work- and how i would use them with this program. Thanks!!

    Jenna

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's like an if statement. Post your coding attempt at it, and advise will follow.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    ?

    can i use if statements for this instead of switch statements? I understand if statements and I would rather use them if I can. I was told switch statements would be easier. Thanks.

    Jenna

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If it helps, post your attempt using an if...else tree, and perhaps then we can show a switch equivalent.

    Or better yet -- post your "easy" if...else version, and then your attempt at converting it to a switch equivalent.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    if(apple==2){
        printf("Wow, there's two apples");
    }
    else if(apple==3{
        printf("Three apples! Yay!");
    }
    else if(apple==4){
        printf("So many apples!");
    }
    else{
        printf("Apple...");
    }
    equals to:
    Code:
    switch(apple){
        case 2:
            printf("Wow, there's two apples");
            break;
        case 3:
            printf("Three apples! Yay!");
            break;
        case 4:
            printf("So many apples!");
            break;
        default:
            printf("Apple...");
            break;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Thank you!

    Thank you for your code example. I can see the relation now! Thanks!!!

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    Cool

    Quote Originally Posted by ammochck21
    Hello-

    I have to write a program that converts numbers into words. I know I have to use switch statements for this but I dont really understand the switch statement to begin with and how it workds and in turn I dont understand how I would use them for this program. Could someone help with an explanation? I dont want a program wirrten- just how switch statements work- and how i would use them with this program. Thanks!!

    Jenna
    A switch statement is often not the only option. If the numbers you are using are all consecutive then you can use a simple array lookup.
    This works especially well if you use enums for your item types.
    Code:
    enum fruitType {APPLE, BANANA, CHERRY};
    const std::string fruitName[] = {"Apple", "Banana", "Cherry"};
    
    fruitType f = BANANA;
    cout << fruitName[static_cast<int>f];
    Easy as ABC.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  3. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM
  4. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM