Janko
Janko

Reputation: 23

I have a switch inside a case. How to exit that switch and repeat case that contains that switch?

I have nested switch statements which are used to navigate the menus inside the app. For example, if I enter quadrangle part of switch the menu is shown and user is prompted to enter a number which corresponds to some calculations regarding the quadrangle type. But, if I mistakenly selected quadrangle instead of triangle, I want to go back to previous menu where I can select desired shape (triangle or quadrangle).

do
{
  // show main menu & get user input
  switch (choice)
  {
    case 1:
      // show shape menu & get user input for shape selection
      switch (shape)
      {
        case 0:
          // exit this switch and begin new iteration of do-while
        case 1:
          // show triangle menu & get user input for triangle case selection
          switch (triangle)
          {
            // cases
          }
          break;
        case 2:
          // show quadrangle menu & get user input for quadrangle case selection
          switch (quadrangle)
          {
            case 0:
              // exit quadrangle switch and enter case 1 of choice switch
              break;
            case 1:
              // do something with square
              break;
            case 2:
              // do something with rectangle
              break;
          }
          break;
        default:
          // do something
          break;
      }
  }
} while (choice != 0);

Additional clarification:

// main menu
Console.WriteLine("1) Make new shape");
Console.WriteLine("2) Read all shapes");
Console.WriteLine("3) Delete shape");
Console.WriteLine("0) Exit");

// shape menu (option 1 from main menu)
Console.WriteLine("1) Triangle");
Console.WriteLine("2) Quadrangle");
Console.WriteLine("0) Back to main menu");

// quadrangle menu (option 2 from shape menu)
Console.WriteLine("1) Square");
Console.WriteLine("2) Rectangle");
Console.WriteLine("0) Back to shape menu");

So, when I reach quadrangle menu and enter 0, I want to go back to shape menu. In other words, when I reach case 2 of shape switch, I want to be able to go back to case 1 of choice switch.

Upvotes: 0

Views: 429

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112392

Since the nested switches are embedded in a do-loop with a while (choice != 0) at the end, this should happen automatically if you set choice to a value unequal 0. You just need a way to suppress the main menu and user input. Introduce a flag (a Boolean variable) for this.

In the nested switch (or in fact in any switch), set choice = 0; when you want to end the loop and set choice = a_value_not_equal_0; if you want to pass the control to the choice switch again.

To answer you concrete case:

bool getMainUserInput = true;   // <===============
int choice = 0;
do
{
  if (getMainUserInput) {       // <===============
    // show main menu & get user input
  }
  getMainUserInput = true; // Set as default for next loop <===============
  switch (choice)
  {
    case 1:
      // show shape menu & get user input for shape selection
      switch (shape)
      {
        case 0:
          // exit this switch and begin new iteration of do-while
        case 1:
          // show triangle menu & get user input for triangle case selection
          switch (triangle)
          {
            // cases
          }
          break;
        case 2:
          // show quadrangle menu & get user input for quadrangle case selection
          switch (quadrangle)
          {
            case 0:
              // exit quadrangle switch and enter case 1 of choice switch
              choice = 1; // loop again     <===============
              getMainUserInput = false;  // <===============
              break;
            case 1:
              // do something with square
              break;
            case 2:
              // do something with rectangle
              break;
          }
          break;
        default:
          // do something
          break;
      }
  }
} while (choice != 0);

Upvotes: 1

Related Questions