Home > Mobile >  Whats the difference between a range of a set(a,b) and union of all elements of sets in (a,b)?
Whats the difference between a range of a set(a,b) and union of all elements of sets in (a,b)?

Time:08-12

Beginner here. Wrote a program to check if entered alphabet is lowercase or upppercase. (Practising if/else questions) I thought of two methods:

  1. Range method-getting the desired result
  2. Union method-not getting the desired result

Here is the code,

if (enteredAlphabet>='a' && enteredAlphabet <='z')
{
    cout<<enteredAlphabet<<" is lowercase.";
}

and

if (enteredAlphabet==('a'||'b'||'c'||'d'||'e'||'f'||'g'||'h'||'i'||'j'||'k'||'l'||'m'||'n'||'o'||'p'||'q'||'r'||'s'||'t'||'u'||'v'||'w'||'x'||'y'||'z'))
{
    cout<<enteredAlphabet<<" is lowercase.";
}

CodePudding user response:

The expression 'a'||'b'||... is actually a short-hand for ('a' != 0) || ('b' != 0) || ....

The result of the expression will always be the boolean value true.

Since true can be converted to the integer value 1 your comparison is really the same as enteredAlphabet == 1. This comparison will likely never be true.

What you actually should use is std::islower:

if (std::islower(enteredAlphabet)) { /* Is lower case letter */ }
  •  Tags:  
  • c
  • Related