For() inside switch

This is a discussion on For() inside switch within the C++ Programming forums, part of the General Programming Boards category; Is it possible to put a for() loop inside a switch statement? Code: #include <iostream> #include <stdlib.h> #include <string> using ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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
    2,864
    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
    856
    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!
    Compiler MSVC++ 2013 with Code::Blocks.

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, 08: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, 05:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

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