Thread: Using the Fall Through Feature of a Switch statement

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    Using the Fall Through Feature of a Switch statement

    Just wanted to share this little beautiful illustration of the "fall through" feature of a switch statement, and how it can be used. (and why you need the break statement after every case, if you don't want it)

    This is the way I remember it working.

    Code:
    /* The Twelve Days of Christmas lyrics, showing the "fall through" feature of the switch statement 
    */
    
    #include <stdio.h>
    
    int main(void) {
    
      int day;
      char numbers[][13]={"empty","first","second","third","fourth","fifth","sixth","seventh","eight","ninth","tenth","eleventh","twelfth"};
      printf("\n\n\n\t\t   Welcome to the Twelve Days of Christmas");
    
      for(day = 1; day < 13; day++) {
        printf("\n\n On the %s Day of Christmas, My True Love Gave to Me:\n", numbers[day]);
    
        switch(day) {
          case 12: printf("\n 12 Drummers Drumming");
          case 11: printf("\n 11 Pipers Piping");
          case 10: printf("\n 10 Lords a'Leaping");
          case 9: printf("\n  9 Ladies Dancing");
          case 8: printf("\n  8 Maids a'Milking");
          case 7: printf("\n  7 Swans a'Swimming");
          case 6: printf("\n  6 Geese a'Laying");
          case 5: printf("\n  5 Golden Rings");
          case 4: printf("\n  4 Calling Birds");
          case 3: printf("\n  3 French Hens");
          case 2: printf("\n  2 Turtle Doves");
          case 1: 
            if(day > 1) 
              printf("\n  And a Partridge in a Pear Tree");
            else
              printf("\n  A Partride in a Pear Tree");
        }
        if(day < 12) {
          printf("\n\n\t\t   press enter to see the next day's presents");
          getchar();
        }
      }
      
      printf("\n\n\t\t\t     press enter when ready");
      day = getchar();
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Nice ,

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Salem View Post
    Code:
       register n = (count + 7) / 8;      /* count > 0 assumed */
    
       switch (count % 8)
       {
       case 0:        do {  *to = *from++;
       case 7:              *to = *from++;
       case 6:              *to = *from++;
       case 5:              *to = *from++;
       case 4:              *to = *from++;
       case 3:              *to = *from++;
       case 2:              *to = *from++;
       case 1:              *to = *from++;
                          } while (--n > 0);
       }
    I don't get it. If count % 8 is anything other than 0, shouldn't you have a do-while loop that's missing the do part?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    If count % 8 is anything other than 0, shouldn't you have a do-while loop that's missing the do part?
    No, you would not. The case labels are just labels, so you can imagine a situation where a goto jumps into the middle of a do while loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  2. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  3. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  4. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  5. Logical error in Switch Statement..
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 05-19-2003, 10:15 PM