简体   繁体   中英

Use switch cases in an if statement

Can we use a condition to include cases in a switch statement?

My code:

switch(menu_option)
{
  if (g_logged_in)
  {
     case 'L':
       cout << "Login Menu\n";
       break;
     case 'S':
       cout << "Signup Menu\n";
       break;
  }
  case 'A':
    foo();
    break;
  case 'B':
    bar();
    break;

  default:
    cout << "Invalid Option!\n";
}

When I compile the file, I get an error

[pewpew@Sierra cpp-something]$ c++ src/main.cc -o main.out
src/main.cc: In function 'int main()':
src/main.cc:53:13: warning: statement will never be executed [-Wswitch-unreachable]
   53 |             if (g_logged_in)
      |             ^~

Is it not at all possible?

Currently, program control never reaches if (g_logged_in) , so that's never evaluated. Your helpful compiler is issuing a warning.

Really though a switch block is little more than a controlled set of goto statements. You could force it with

switch(menu_option){
    foo:
    if (g_logged_in){

with a goto foo; somewhere where program control reaches, although such an idea is ill-advised due to readability and maintainability. The following snippet is a reformulation of the "Hello World!" program:

#include <iostream>
int main(){
    switch (1){
    foo:
    if (true){
        case 0:
            std::cout << " World!\n";
            break;
        case 1:
            std::cout << "Hello";
            goto foo;
        }
    }   
}

For a real-life use-case for interleaved control loops within a switch block, see Duff's Device: https://en.wikipedia.org/wiki/Duff%27s_device

there is no "case" above your code. You can't do this. The case is most likely compiled into a CPU instruction, that only works on integers. You must use the case and do the if() twice for both cases.

You cannot specify case statements conditionally. You would have to do something more like this:

switch (menu_option)
{
  case 'L':
    // in C++17 and later, you can uncomment the following
    // attribute to avoid a compiler warning...
    //
    // [[fallthrough]];
  case 'S':
    if (g_logged_in)
    {
      if (menu_option == 'L')
        cout << "Login Menu\n";
      else
        cout << "Signup Menu\n";
    }
    else
      cout << "Invalid Option!\n";
    break;

  case 'A':
    foo();
    break;

  case 'B':
    bar();
    break;

  default:
    cout << "Invalid Option!\n";
    break;
}

Or this:

switch (menu_option)
{
  case 'A':
    foo();
    break;

  case 'B':
    bar();
    break;

  default:
    if (g_logged_in)
    {
      if (menu_option == 'L')
      {
        cout << "Login Menu\n";
        break;
      }
      if (menu_option == 'S')
      {
        cout << "Signup Menu\n";
        break;
      }
    }
    cout << "Invalid Option!\n";
    break;
}

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