It would be elegant if the language permitted switching on a string, but it
doesn't. In my mind, cascading if-else statements are not inelegant if they
are what is required to get the job done. If you are concerned about
efficiency (as you should be), you should take advantage of the else clause,
and put the most common matches first (if they can be determined in
advance). This will minimize the number of calls to strcmp().
if (strcmp(value,"John")==0)...
else if (strcmp(value,"Tom" )==0)...
else if (strcmp(value,"Mary")==0)...
Without the else clause, you'll call strcmp N (9 in your case) times
regardless of the value, when a single call might have been sufficient.
As Thom Bentley points out, you can use a switch if you are willing to jump
through sufficient hoops, but it may well use more CPU time to do so.
-----Original Message-----
From: XML Man [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 18, 2001 9:47 AM
To: [EMAIL PROTECTED]
Subject: getNodeValue().transcode() - Explanation
Thank you everybody.
I only typed two possibilities ("John" and "Tom") only not
to write too much, but the fact is that I've got 9
possibilities, and I do not want to use if-then-else, but
switch-case.
I tested with:
value = attr.getNodeValue().transcode()
if (strcmp(value,"John")==0)...
if (strcmp(value,"Tom")==0)...
if (strcmp(value,"Mary")==0)...
It works but, in my opinion, it's more elegant switch-case,
isn't it?
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]