Thread: Nested Switch

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47

    Nested Switch

    I have a control program that uses a nested Switch Statement. It gets lost while executing the second Switch.

    The code looks like this:

    Code:
    int A;
    int B = x;  //B, C, D and E are constants
    int C = y;
    int D = z;
    int E = w;
     while(1){
    
       A = (input value)
       RANGE = (input value)
       SELECT = (input value)
       SELECT2 = (input value)
    
            switch (RANGE){
    
                 case 1: //Do something
                  break;
                case 2: //Do something
                  break;
                case 3: //Do something
                  break;
                case 4: //Do something
                  
                     switch (SELECT) {
          
                      case 1: 
                          if (A < B) //Do something
                           else
                           if (A >= B) //Do something else
                  
                       break;
    
                    case 2:
                         if (A < C) //Do something
                          else
                        if (A  >= C) //Do somthing else
    
                       break;
    
                    case 3:
                       if (A < D) //Do something
                          else
                        if (A  >= D) //Do somthing else
    
                       break;
    
                     case 4:
                       if (A < E) //Do something
    
                        }
    
                       break;
    
               case 5:    switch (SELECT2) {
    
                     case1: //Do something
                       break; 
    
                     case2: //Do something else
                        } //End switch SELECT2
                       break; 
    
              case 6: //Do something
    
                     } //End   switch SELECT  
    
                    } //End switch RANGE
    
                  } //End While
    Last edited by danlee58; 11-12-2013 at 08:45 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And by "gets lost" you mean what exactly?

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    When I say that it gets lost, I mean that the hardware doesn't switch to the proper state, based on the input conditions. It starts out right, but when it gets to case 2 under case 4 it doesn't switch properly. BTW there is a typo in case4. It should be case 4:, not case 4;

    I have corrected that, but it never was in the original code.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That probably means that either (a) your variables don't have the values you think they do or (b) your communication with the hardware isn't communicating. There's nothing wrong with the logic as you have it here.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Any decent hardware development environment should have a debugger, so you can step through your code and see how it is being executed. Have you looked into this route yet?

  6. #6
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    I think that I see a problem here:

    Code:
       case 4:                    
             if (A < E) //Do something                       }                     
             break;              
            case 5:    switch (SELECT2) {                    
                          case1: //Do something                    
                           break;                     
                          case2: //Do something else                     
                           } //End switch SELECT2                   
                            break;              
            case 6: //Do something                    
                 // } //End   switch SELECT  This should be before switch SELECT2                 
                   } //End switch RANGE                 
                   } //End While

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    I don't have a debugger. I have to compile & test the code on the hardware. I use 'printf' to debug.

    I also don't have an editor that will track (, and ) or {. and }.
    Last edited by danlee58; 11-12-2013 at 09:09 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your end switch SELECT is on line 49 of your original posting. You didn't label it, but that's what it's doing. Granted it may not function that way in your original code (ie it might be the end brace of your if), but only you can actually see the code to know what lines up with what.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by danlee58
    I have a control program that uses a nested Switch Statement. It gets lost while executing the second Switch.
    I recommend that you move the nested switch code into a separate function to avoid getting lost yourself while reading your own code.
    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

  10. #10
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Quote Originally Posted by laserlight View Post
    I recommend that you move the nested switch code into a separate function to avoid getting lost yourself while reading your own code.
    You are right, and I have considered that myself, but I like to see the active code on one screen or page, so I don't have to jump back & forth while reading the code.

    The code that I posted is simplified for the purpose of posting here. In the actual code the constants B, C, D, and E are arrays of constants, and the //Do somthing is a Function that looks up the value. So if I make the nested switch a Function, I will have a Funtion that calls a Funtion. While there is nothing wrong with that, I think that it would be more confusing than the nested switch.

    Actually the simplified version of the code that I posted here is really what I needed to do for myself, as well as for posting.

    Thanks to everyone for the help.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you don't want to use a separate function, use additional braces around the inner switch. Reduce your confusion of what the effect of each break statement is.

    Worst case is that you'll need some additional logic if, from the inner switch, you need to back out of both.

    I'd prefer putting it in a function like laserlight suggested, as backing out can be handled by a simple return code rather than keeping track of what break jumps where. If a function is self-contained, you would not need to jump back and forth between screens to understand what is going on - the program logic would usually be simpler than you have.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 10:29 PM
  2. A nested if inside the switch
    By Extropian in forum C Programming
    Replies: 20
    Last Post: 08-15-2005, 01:23 AM
  3. nested switch
    By kurz7 in forum C Programming
    Replies: 1
    Last Post: 09-07-2003, 09:32 AM
  4. Help with Nested Switch
    By liquidspaces in forum C++ Programming
    Replies: 9
    Last Post: 03-28-2003, 04:08 PM
  5. nested switch
    By ccmac in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2002, 09:19 PM