Switch statement using or

2019-01-18 18:09发布

I'm creating a console app and using a switch statement to create a simple menu system. User input is in the form of a single character that displays on-screen as a capital letter. However, I do want the program to accept both lower- and upper-case characters.

I understand that switch statements are used to compare against constants, but is it possible to do something like the following?

switch(menuChoice) {
    case ('q' || 'Q'):
        //Some code
        break;
    case ('s' || 'S'):
        //More code
        break;
    default:
        break;
}

If this isn't possible, is there a workaround? I really don't want to repeat code.

6条回答
祖国的老花朵
2楼-- · 2019-01-18 18:46

'q' || 'Q' results in bool type result (true) which is promoted to integral type used in switch condition (char) - giving the value 1. If compiler allowed same value (1) to be used in multiple labels, during execution of switch statement menuChoice would be compared to value of 1 in each case. If menuChoice had value 1 then code under the first case label would have been executed.

Therefore suggested answers here use character constant (which is of type char) as integral value in each case label.

查看更多
干净又极端
3楼-- · 2019-01-18 18:46
switch (toupper(choice))
{
  case 'Q':...
}

...or tolower.

查看更多
戒情不戒烟
4楼-- · 2019-01-18 18:47

The generally accepted syntax for this is:

switch(menuChoice) {
    case 'q':
    case 'Q':
        //Some code
        break;
    case 's':
    case 'S':
        //More code
        break;
    default:
        break;
}

i.e.: Due the lack of a break, program execution cascades into the next block. This is often referred to as "fall through".

That said, you could of course simply normalise the case of the 'menuChoice' variable in this instance via toupper/tolower.

查看更多
SAY GOODBYE
5楼-- · 2019-01-18 18:50

This way:

 switch(menuChoice) {
    case 'q':
    case 'Q':
        //Some code
        break;
    case 's':
    case 'S':
        //More code
        break;
    default:
 }

More on that topic: http://en.wikipedia.org/wiki/Switch_statement#C.2C_C.2B.2B.2C_Java.2C_PHP.2C_ActionScript.2C_JavaScript

查看更多
祖国的老花朵
6楼-- · 2019-01-18 19:03

You could (and for reasons of redability, should) before entering switch statement use tolower fnc on your var.

查看更多
Explosion°爆炸
7楼-- · 2019-01-18 19:06

Just use tolower(), here's my man:

SYNOPSIS
#include ctype.h

   int toupper(int c);
   int tolower(int c);

DESCRIPTION toupper() converts the letter c to upper case, if possible.

   tolower() converts the letter c to lower case, if possible.

   If c is not an unsigned char value, or EOF, the behavior of these
   functions is undefined.

RETURN VALUE The value returned is that of the converted letter, or c if the conversion was not possible.

So in your example you can switch() with:

switch(tolower(menuChoice)) {
    case('q'):
        // ...
        break;
    case('s'):
        // ...
        break;
}

Of course you can use both toupper() and tolower(), with capital and non-capital letters.

查看更多
登录 后发表回答