Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Long IF statement

Status
Not open for further replies.

pjb

Programmer
May 1, 2001
148
US
I have to compare a string value to about thirty possibilities. On way would be a long IF OR combination, something like this:
If ((!strcmp(fielda,"AAA")) || (!strcmp(fielda,"BBB")) || (!strcmp(fielda,"CCC")) ..........).

Is there a slicker way?
 
Well, it depends on the possablities you need to compare it to.

Suppose you know that it will use just the characters

ABCDEFGHIJKLMNOP

you can then do a strspn on it with that character set and if there are invalid characters you know not to process it.

Also, there exists a finite number of valid strings of which you could create special cases. This approach is too much work if it is just 30 or so strings and I would say stay with the big if condition. If that seems to look a bit nasty, you could alway move the if condition to a funciton called ValidString(char* ptr) and return if it is valid or not.

Matt
 
I think I might use an array to hold my values, and then spin thru the array using only one if statement. I know the values are always three characters long, so I could set the array up like this:

static char my_array[]={"AAA","BBB","CCC",......};

The loop can go like this:
for (x=0;x<sizeof(my_array)/5;x++)
{
if (!strcmp(field1,my_array[x]))
{ Do whatever
}
}
 
To improve upon that idea a bit, you could use bsearch() to do a binary search on the array rather than a linear search.

On a side note, to get the number of elements in an array, you can do this:

sizeof array / sizeof *array

You can wrap this up in a macro:

#define ARRAY_SIZE(A) (sizeof (A) / sizeof *(A))

Beware: this doesn't work with pointers.
Russ
bobbitts@hotmail.com
 

Hey Dude,
This is Good logic(int value = 'A' | 'A'<<8 | 'A'<<16;
)
, But for each and every String there is a need of doing bit operations. Instead of doing like this it is better to use if conditions using strcmp or strcasecmp. Am i right?

Amarnath Revuri.
 
Yes, but placing the strings in an array (like in pjb's example) and doing the comparisons in a loop is easier to write and maintain.

Anyway, I doubt Zyrenthian was being serious in his example :)

Russ
bobbitts@hotmail.com
 
Try following:

boolean swichtval=false;

Switchwal |= (Strcmp(str1,checkstring1)=0);
Switchwal |= (Strcmp(str1,checkstring2)=0);
Switchwal |= (Strcmp(str1,checkstring3)=0);

.
.
.
if(switchval)
{
.....

If it is necessary, you could store all the strings in an array, and perform the initial checks within a loop.

hnd
hasso55@yahoo.com

 
@rbobitt,

Ok.
Then use
int switchval=0;

;)
hnd
hasso55@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top