PDA

View Full Version : Too many ifs? Better way?

Morgoth
02-22-2003, 05:56 AM
Ok, I have a system set up to read a title, or a name for a certain product in something like a table.

An example of that table is here(save as .html to view):

<table border="1">
<tr>
<td align="center">0</td>
<td align="center">0-10</td>
<td align="center">10-20</td>
<td align="center">20-30</td>
</tr>
<tr>
<td align="center">0-10</td>
<td>Name 1A</td>
<td>Name 2A</td>
<td>Name 3A</td>
</tr>
<tr>
<td align="center">10-20</td>
<td>Name 1B</td>
<td>Name 2B</td>
<td>Name 3B</td>
</tr>
<tr>
<td align="center">20-30</td>
<td>Name 1C</td>
<td>Name 2C</td>
<td>Name 3C</td>
</tr>
</table>

[Y(v=down) X(>=Right)]
Now, I don't LITERALLY have a table set up like this, I have a function that looks like this (all if statements):

If X => 0 And X =< 10 Then
If Y => 0 And Y =< 10 Then
Name = 1A
ElseIf Y => 10 And Y =< 20 Then
Name = 2A
ElseIf Y => 20 And Y =< 30 Then
Name = 3A
End If
ElseIf X => 10 And X =< 20 Then
If Y => 0 And Y =< 10 Then
Name = 1B
ElseIf Y => 10 And Y =< 20 Then
Name = 2B
ElseIf Y => 20 And Y =< 30 Then
Name = 3B
End If
ElseIf X => 20 And X =< 30 Then
If Y => 0 And Y =< 10 Then
Name = 1C
ElseIf Y => 10 And Y =< 20 Then
Name = 2C
ElseIf Y => 20 And Y =< 30 Then
Name = 3C
End If
End If

Now first off, just so you know, because I chose 1A, 1B, 1C, 2A, etc, does not mean there is a pattern in what 'Name' equals. Name acually equals a unique string.

And so what I am trying to ask and find out, is if there is a much easier way to do this. Meaning not use all these if statements in this sort of way. Is there a better way?

Any suggestions will help.

Morgoth
02-22-2003, 07:17 AM
Forget about this, there is more to it, and I am unable to explain it better. whammy, you should probibly close or delete this post.

whammy
02-23-2003, 03:01 AM
Not without making a suggestion first - although it has nothing to do with your question, you should get used to doing this differently:

If X => 0 And X =< 10 Then

... which really should be:

If X >= 0 And X <= 10

The only reason I suggest you start doing it this way, is all of the programming languages (which is most of them, FYI) that use C or C++ type syntax (like JavaScript, PHP, C, C++, Java, C#, etc.) use the latter format and would throw an error otherwise, since those operators wouldn't be understood (or compile). VBScript is sloppy in this regard by allowing developers to code this way.

"Less than or equal to"
"Greater than or equal to"

Is the common usage.

So if you don't break yourself of the habit now, you will get a LOT of errors venturing into other languages, and I know you will venture into other languages! And the worst thing about this kind of error is it's not readily apparent unless you're using a debugger which highlights this for you (for instance in javascript it might be hard to spot).

P.S. If you want to delete the post, all you have to do is edit your first post and delete it yourself. ;)

whammy
02-23-2003, 03:04 AM
P.S. some of those conditions are not necessary, since you have already checked for them in previous if/then statements... ;)

You'd only have to see if it was LESS than or EQUAL to something after the first one with an elseif, see...

Morgoth
02-23-2003, 04:31 AM
The Greater than and Less than fault was just a typing mistake, this isn't the exact code I am using, it is much larger and would just take up alot of roon if I posted it.

Hum, accually, the idea of the code got alot more confusing as I went into it, and I got a bit of help from my brother, but I had to use.

The numbers range from -10,000 to 10,000 and I need to note where X and Y are between certain numbers, numbers which double.

X could equal: Between ( 0 to [1,250) to (2500] to [5,000) to 10,000]
And
Y could equal: Between (-10,000 to [-5,000) to (-1,250] to [-625) to (625] to [1,250) to [5,000] to 10,000]
(I am bored ^_^)

So what my brother helped me make was this in VB using math, and an array.

I am going to look over this, and get it to work for more then just X.

X = Some number between -10,000 and 10,000
If X <> (A Positive Number) Then
Write: Abs(Int(Log(Abs(X) / 625) / Log(2)) - 4)
Else
Write: Int(Log(X / 625) / Log(2)) + 6
End If

This is return 0 to 10 And in my array, I will have Array(X,Y) and a List for X to Y to pick from. 5 is (X=624 to -624) since 5 is never listed..

If you have VB you can test this:

First Make a new Form, and create a "LISTBOX", don't bother to change the name or remove/add anything to it's props.