This question already has an answer here:
Just instead of :
if ( ch == 'A' || ch == 'B' || ch == 'C' || .....
for example, to do it like :
if ( ch == 'A', 'B', 'C', ...
is there even a shorter way to summarize conditions ?
|
This question already has an answer here: Just instead of :
for example, to do it like :
is there even a shorter way to summarize conditions ? |
||||
marked as duplicate by Olaf, 1201ProgramAlarm, Hong Ooi, BlueRaja - Danny Pflughoeft, zespri 50 mins agoThis question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. |
||||
|
|
|||||||||||||||||||||
|
|
In this case you could use a
Note that this won't work as you might expect because of the use of the comma operator:
This first compares |
||||
|
|
|
templates!
|
|||||||||||||||||||||
|
|
If you need to check a character against an arbitrary set of characters, you could try writing this:
|
|||||||||||||||||||||
|
|
The X-Y answer on the vast majority of modern systems is don't bother. You can take advantage of the fact that practically every character encoding used today stores the alphabet in one sequentially-ordered contiguous block. A is followed by B, B is followed by C, etc... on to Z. This allows you to do simple math tricks on letters to convert the letter to a number. For example the letter C minus the letter A , 'C' - 'A', is 2, the distance between c and a. Some character sets, EBCDIC was discussed in the comments above, are not sequential or contiguous for reasons that are out of scope for discussion here. They are rare, but occasionally you will find one. When you do... Well, most of the other answers here provide suitable solutions. We can use this to make a mapping of letter values to letters with a simple array:
So No if statements or conditional logic required what-so-ever. All the debugging you need to do is proof reading As you study more in C++, you will learn that Using this array could be as simple as
But this has two fatal blind spots: The first is capital letters. Sorry Ayn Rand, but 'A' is not 'a', and
The other is input of characters that aren't letters. For example, '1'. Fortunately this also has a simple fix: Only compute the score for good input. You can test for valid alphabet characters with
My something else would be |
|||
|
|
|
Similarly to the C
The above will return This works because Edit: There's another C++ method which doesn't involve dynamic allocation, but does involve an even longer piece of code:
This works similarly to the first code snippet: |
|||||||||||||||||||||
|
|
There is solution to your problem, not in language but in coding practices - Refactoring. I'm quite sure that readers will find this answer very unorthodox, but - Refactoring can, and is used often to, hide a messy piece of code behind a method call. That method can be cleaned later or it can be left as it is. You can create the following method:
and then this method can be called in a short form as:
Reuse that method with so many checks and only returning a boolean, anywhere. |
||||
|
|
|
Yet another answer on this overly-answered question, which I'm just including for completeness. Between all of the answers here you should find something that works in your application. So another option is a lookup table:
Scoff at the C-style static casts if you must, but they do get the job done. I suppose you could use an Obviously this becomes cumbersome with e.g. |
|||||
|
|
For this specific case you can use the fact that
But in general this is not possible unfortunately. If you want to compress your code just use a boolean function
|
|||||||||||||
|
|
If you want to check for For And for |
|||||
|
|
One option is the
|
|||
|
|
isupper(ch)? – Barmar 13 hours agoif (ch >= 'A' && ch <= 'C')or wherever it goes to. – Weather Vane 13 hours ago'A' < 'B' < ... < 'Z'and'a' < 'b' < ... < 'z', but indeed does not require contiguity, nor does it specify the ordering of'A'and'a'. The ordering requirement is not explicitly stated, but can be derived from the combination of the rules for the basic execution character set and the rules for the "C" locale. – zwol 13 hours ago