wilbomc
wilbomc

Reputation: 183

Sharing a case in a switch statement. Expression issues. How does it work?

On an example I am working on, I have code to pick out the vowels between 'a' to 'z'. It is using a switch statement that instead of separate cases, the character values are sharing the same case. From what I know so far, the expression involved i.e

***(letter * (letter >= 'a' && letter <= 'z'))*** 

evaluates to true or false and is converted to an integer (1 and 0) in which it falls through to "case 0:" (0 obviously being false) to deal with the result should it be false. Can anyone explain the expression to conversion process involved with this statement? particularly the reasoning behind the multiplication of the logical expression. Here is my example code:

char letter(0); 
cout << endl
     << "Enter a small letter: ";
 cin >> letter;

switch(letter * (letter >= 'a' && letter <= 'z'))
{
case 'a': case 'e': case 'i': case 'o': case 'u':
    cout << endl << "You entered a vowel.";
    break;

case 0:
        cout << endl << "That is not a small letter.";
        break;

    default: cout << endl << "You entered a consonant.";
}

EDIT: All great answers guys. Cleared a lot up. Thanks again for your input

Upvotes: 1

Views: 473

Answers (3)

David G
David G

Reputation: 96845

letter * (letter >= 'a' && letter <= 'z')

The conditional expression multiplicand will evaluate to a true or false. The ASCII value of letter will be compared to the characters "a" and "z". True and false can be implicitly-converted to the numerals 1 and 0 respectively (and letter will be converted to its numeric ASCII value). So it will either be:

letter * (0)

or

letter * (1)

Anything times 0 is 0 which means without a corresponding case the control will go down to the default case (this one goes down to the case: 0 part). Otherwise, letter * (1) is letter, and therefore it will be compared by the other cases as if it were switch (letter).

Upvotes: 2

Karthik T
Karthik T

Reputation: 31952

(letter * (letter >= 'a' && letter <= 'z'))

Lets take it in 2 parts.

(letter >= 'a' && letter <= 'z')

This one is easy. Is the ascii code of letter greater than that of a and less than that of z. Since letters are sequencially increasing in ascii, this means that the letter is a small letter. However the result of this expression is a true/false, which translates to 1/0 in the next stage.

(letter * <1/0>) 

At this stage, letter * 1 returns letter and letter * 0 returns 0. Which explains how the case statements work.

For the exact same reason that brought you here (lack of readability), I would split this off into a if first which checks for lower case letter, before passing on to the switch.

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126867

(letter >= 'a' && letter <= 'z') is a boolean expression, which evaluates to true if the letter is between 'a' and 'z'; now, when you multiply it by letter, it first gets promoted to char, assuming the value 1 if it's true, 0 if false.

Thus, the whole expression will yield 0 if the condition in the parentheses evaluates to false, or letter if it evaluates to true, since any letter multiplied by zero yields zero, and multiplied by 1 yields itself.

Upvotes: 1

Related Questions