Arrays can replace most "if" statements
The forum has helped me out alot
specifically with the help of Peter Verkaik,
he is a great guy. Thank you Peter.
I thought I would give something back with
a little trick replacing most of your
"if" statements with an array.
Example:
if (shoesize == 1)
{ans = "This is a small foot"}
else
if (shoesize == 9)
{ans = "This is an average size foot"}
else
if (shoesize == 16)
{ans = "This is Bigfoot!"}
MyAnswer = ans;
Replace with:
static String shoeSizes [noparse]/noparse =
{"","This is a small foot",.......,"This is an average size foot", ETC};
Then your code looks like this:
·myAnswer = shoeSizes[noparse][[/noparse]shoesize];
It processes faster, takes up fewer lines of code, and makes your code neater.
As you can see, it works great when you have lookups with small
index numbers fairly close to each other. You need to fill every index
range for it to work properly.
Happy Coding!
Jeff (trytryagain)
·
specifically with the help of Peter Verkaik,
he is a great guy. Thank you Peter.
I thought I would give something back with
a little trick replacing most of your
"if" statements with an array.
Example:
if (shoesize == 1)
{ans = "This is a small foot"}
else
if (shoesize == 9)
{ans = "This is an average size foot"}
else
if (shoesize == 16)
{ans = "This is Bigfoot!"}
MyAnswer = ans;
Replace with:
static String shoeSizes [noparse]/noparse =
{"","This is a small foot",.......,"This is an average size foot", ETC};
Then your code looks like this:
![cool.gif](http://forums.parallax.com/images/smilies/cool.gif)
It processes faster, takes up fewer lines of code, and makes your code neater.
As you can see, it works great when you have lookups with small
index numbers fairly close to each other. You need to fill every index
range for it to work properly.
Happy Coding!
Jeff (trytryagain)
·
Comments
Strings do have quite a overhead due to the way stringtables are coded.
Another alternative is to use switch
regards peter