Looong IF statement.. help

• 12-08-2004
jstevanus
Looong IF statement.. help
I have a very long IF Statement that doesn't seem to be working. What is the proper way to code this:

Code:

```      if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' ||       c == 'F' c == 'G' || c == 'H' || c == 'I' || c == 'X')```

Josh Stevanus
jrstevan@svsu.edu
• 12-08-2004
Frantic-
i dont know much about c++ my self, but shouldnt you be using double quotes?

" value "
• 12-08-2004
alpha
Quote:

Code:

```if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' ||       c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'X')```

only problem i see, without more code.
• 12-08-2004
Frantic-
Quote:

Originally Posted by alpha
only problem i see, without more code.

:D wow i missed that my self. good eye. Im pretty sure that would fix your problem.
• 12-08-2004
you could try something like this:

if( (c >= 'A' && c <= 'I') || c == 'X')

but the logic gets pretty complex sometimes, so sometimes it's just easier to write out the entire list.
• 12-08-2004
The Brain
I personally like to handle long IF statements by using the cascading property of switch/case:

Code:

```char c; cout << "Enter Choice"; cin >>  c;     switch(toupper(c))     {           case 'A':           case 'B':           case 'C':           case 'D':           case 'E':           case 'F':           case 'G':           case 'H':           case 'I':           case 'X':  do something;           default :  else do something;     }```

--Or you can try this--

When you have a list of consecutive characters, you could just use a range of ascii values.. instead of looking for specific characters.

I'm not totally positive on how to do this in c++.. so someone please back me up on this
Code:

```//IF c is between the ascii value of A and below the ascii value of I  OR  c equals the ascii value of X if ((c >= 41h && c <=49h) || (c == 58h))```
Which I think can also be written like this:
Code:

`if ((c>='A' && c<='I') || (c=='X'))`
which is much easier to write than all of this:
Code:

```if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' ||       c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'X')```

optimization == good :cool:

 I am slow.
• 12-08-2004
jlou
Quote:

Originally Posted by Frantic-
i dont know much about c++ my self, but shouldnt you be using double quotes?

" value "

Double quotes are for string literals (zero or more characters), and single quotes are for character literals (one and only one character).

The switch statement makes the most sense in this case, assuming the OP has learned it.
• 12-08-2004
The Brain
good catch.. just went back to edit my mistake :)