Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post

    Too many ifs? Better way?

    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):
    Code:
    <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):

    Code:
    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.

  • #2
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by whammy; 02-23-2003 at 02:13 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post
    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.
    Code:
    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:
    Code:
    First Make a new Form, and create a "LISTBOX", don't bother to change the name or remove/add anything to it's props.
    
    Now add this code:
    
    Private Sub Form_Load()
    Dim I As Integer
    
    For I = -10000 To -625
        List1.AddItem Abs(Int(Log(Abs(I) / 625) / Log(2)) - 4) & " --- " & I
    Next I
    
    For I = 625 To 10000
        List1.AddItem Int(Log(I / 625) / Log(2)) + 6 & " --- " & I
    Next I
    End Sub
    When you press play it may take 5-20 seconds to load, but it will display all the results from -10,000 to 10,000, except between -624 and 624.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •