Please help baby C programmer...

Paolo Carlini pcarlini@unitus.it
Sun Jan 27 16:28:00 GMT 2002


Ron Hudson wrote:

> I am having trouble with the switch / case statement.
>
> In my program I have a nested case statement.
>
> Is is OK to nest switch statements?

Sure! The following implementation of your pseudocode both compiles and run as
expected
(just remember to put a break; also after the end of each of the inner swithes
(if I understand well your intention))

#define AA 1
#define BB 1

int main()
{
  int a = AA;
  int b = BB;

  switch (a){
  case 1:
    switch (b){
    case 1:
      printf("a1b1\n");
      break;
    case 2:
      printf("a1b2\n");
      break;
    } /* close switch b */
    break;
  case 2:
    switch (b){
    case 1:
      printf("a2b1\n");
      break;
    case 2:
      printf("a2b2\n");
      break;
    } /* close switch b */
    break;
  }/* close switch a */

  return 0;
}





More information about the Gcc mailing list