Thread: For() inside switch

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    For() inside switch

    Is it possible to put a for() loop inside a switch statement?

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {  
         for (int i=0; i < 8; i++)
         {
                  switch (i)
                  {
                      case  for (int j=0; i < 6; j++):  //something like this?
                            cout<<"1-6"<<endl;
                            break;
                      case  7:
                            cout<<"7"<<endl;
                            break;
                   }
         }              
        
        system("pause");
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but the for loop cannot be the label, of course.
    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

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Yes, but not the way you did it (as a label).

    try this -

    Code:
                     case 1:
                     case 2:
                     case 3:
                     case 4:
                     case 5:
                     case 6:
                            cout<<"1-6"<<endl;
                            break;
    if you need a very large range, consider using an if statement instead or in conjunction with the switch statement.
    Last edited by abachler; 03-18-2009 at 09:16 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What you have there might better be written as

    Code:
    for (int i = 0; i < 7; ++i) {
        std::cout << "0-6\n";
    }
    std::cout << "7\n";
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the way to do what you're suggesting is to use fallthrough.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {  
      for (int i=0; i < 8; i++)
      {
        switch (i)
        {
          case 0:
          case 1:
          case 2:
          case 3:
          case 4:
          case 5:
          case 6: {
            cout<<"0-6"<<endl;
            break;
          }
          case  7: {
            cout<<"7"<<endl;
            break;
          }
        }
      }              
        
      system("pause");
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Code:
    What you have there might better be written as
    Yes i realized that. I was just trying to rewrite the same program with switch(), but it takes up much more space indeed.

    Thanks everybody!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM