Structure of Switch code

I have problems with a function involving switch statements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Read in message from slave(s) confirming the completion of tasks.
void confirmCompletion()
{
   if (radio.available())
   {
      radio.read(&confirmation, sizeof(confirmation));
   
      if (confirmation[0] == 'X')  // Task is completed.
      {
         if (libraryMasterDebug)
         {
            Serial.print("Task ");
            Serial.print(confirmation[1]);
            Serial.print(" completed by slave");
            Serial.println(confirmation[2]);
         }  // end if
      
         // Set the Finite State Machine flags.
         switch (confirmation[2])  // Slave number.
         {
            case '0':
               switch (confirmation[1])  // Task number.
               {
                  case '1':
                     slave0Task1done = true;
                  case '2':
                     slave0Task2done = true;
                  case '3':
                     slave0Task3done = true;
                    default:
                    	if (libraryMasterDebug)
                    	{
                    		Serial.print
                    		("Invalid task confirmation from slave");
                    		Serial.println(confirmation[2]);
                    	}  // end if
                    	stallTheProgram();
               }  // end case switch (confirmation[1])
            
               slave0waiting = true;
            case '1':
               switch (confirmation[1])  // Task number.
               {
                  case '1':
                     slave1Task1done = true;
                  case '2':
                     slave1Task2done = true;
                  case '3':
                     slave1Task3done = true;
                    default:
                    	if (libraryMasterDebug)
                    	{
                    		Serial.print
                    		("Invalid task confirmation from slave");
                    		Serial.println(confirmation[2]);
                    	}  // end if
                    	stallTheProgram();
               }  // end case switch (confirmation[1])
            
               slave1waiting = true;
         
              // cases must be set for all possible slaves  << ===========
              
              default:
              	if (libraryMasterDebug)
              		Serial.print("Invalid slave number");
              		stallTheProgram();
         }  // end switch (confirmation[2])
      }  // end if (confirmation[0] == 'X')
   }  // end if (radio.available())
}  // end confirmCompletion() 


This code is written to program Arduino stuff so some of the identifiers may look a bit strange. That shouldn't matter, it's C++ structure I am interested in.

Firstly, is it okay to have switch within a switch?

Secondly, is the structure I have correct?

Thirdly, is there any reason (given correct input) why a 'default' should trigger?
Firstly, is it okay to have switch within a switch?

Yes.

Secondly, is the structure I have correct?

Probably not, since:


Thirdly, is there any reason (given correct input) why a 'default' should trigger?

None of the code in your switch cases have a break, so if the code for one case is executed, it will fall through to the next case (until it reaches a break or the end of the compound statement which defines the body of the switch statement) and execute that code too.

Use a break; statement to terminate the code in each case.

Your compiler is likely generating a warning that advises you similarly.
Last edited on
Thanks for that cire.
Leaving out 'break'! Aaaaarrrrrggggh!!
Topic archived. No new replies allowed.