简体   繁体   中英

Can I use a case inside another case in a Switch Case in C

Here, In the Switch Case I am using some key events.

When I press 0 it directs to Case 0: and goes back to the MainWindow I have, by closing the second-one(Window2). And If I press 1 it directs to Case 1 and so on respectively, where the operations will be executed separatedly.

My key events:

    g_printerr("%s\n", gdk_keyval_name (event->keyval));
    keypressed=gdk_keyval_name (event->keyval);
    printf("The KeyEvent is: %s\n", keypressed);     
    char ch[10];
    sprintf(ch, "%s\n", keypressed);   
    printf("The NewKeyEvent is: %s\n", ch);
    int holdch=atoi(ch);

Switch Case:

switch(holdch)
         {
              case 0:
                   printf("Close Window2")
              break;   
              case 1:
                   printf("Open Quadrant1");
              break;
              case 2:
                   printf("Open Quadrant2");
              break;
              case 3:
                   printf("Open Quadrant3");
              break;
              case 4:
                   printf("Open Quadrant4");
              break;
           }        

Now, I want to close the operations getting run from Case(1-4) when running and come back to the Second window from where it left before.

How to do this? Can another Cases be used inside Case(1-4) ? This time I want to use key-buttons like Esc or q for that. Is it possible?

NOTE:

Case 0 -> Close the window where I am now and after this there is no chance of executing other cases. As Second Window is the Window containing 4 Quadrants.

Case(1-4) -> All are executable but can be executed one at a time.

There can be another switch statement inside a cases (nested switch case statements). Example:

switch(keyevent) {
case 0:
    printf("Close Window2")
    break;   
case 1:
   printf("Open Quadrant1");
   switch(foo) {
   case 1:
       // ...
   }
   break;
   // ...
}

But, if I understand your use-case correctly, I don't think you need it. How bout something like that:

switch(keyevent) {
case 0:
    printf("Close Window2")
    break;   
case 1:
    q1_is_open = true;
    printf("Open Quadrant1");
    break;
case 2:
    q2_is_open = true;
    printf("Open Quadrant2");
    break;
case 3:
    q3_is_open = true;
    printf("Open Quadrant3");
    break;
case 4:
    q4_is_open = true;
    printf("Open Quadrant4");
    break;
case Q_BUTTON_PRESSED:
    if (q1_is_open) {
        printf("Close Quadrant1");
        q1_is_open = false;
    }
    if (q2_is_open) {
        printf("Close Quadrant2");
        q2_is_open = false;
    }
    if (q3_is_open) {
        printf("Close Quadrant3");
        q3_is_open = false;
    }
    if (q4_is_open) {
        printf("Close Quadrant4");
        q4_is_open = false;
    }
    break;
} 

If you want to run your main-window and quadrant-window code sequentially you'll need to run whatever you have in cases 1-4 in a separate thread:

How do I start threads in plain C?

The code in that thread will have to have a switch which processes the key strokes that you want to use for exiting it.

You can nest switch statements.

(If this doesn't help then you'll need to re-word your question.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM