Changing from If..Then to Select Case


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Changing from If..Then to Select Case

    Hi,
    Just like for Shawn, my experience is that Select Case is "cleaner" code but takes more space than a bunch of IF/THEN. There's one difference to keep in mind though - which may or may not matter:
    When you "hit" the SELECT statement it's evaluated and the program jumps directly to the correct CASE and executes that. When you have a bunch of IF/THEN each and every IF statement "in the list" is evaluated and the ones evaluated TRUE (if any will execute). With 320 IF statements it's going to take a different amount of time before the actual code in the "correct" IF/THEN block is executed depending on "where" in the list it is.

    As for this code:
    Code:
    If B7 = 065 then Apt = 509: goto DispAlarm1 (DispAlarm1 would be for the first building)
    If B7 = 082 then Apt = 018: goto DispAlarm2 (DispAlarm2 would be for the second building)
    If B7 = 163 then Apt = 107: goto PrntRestore (The PrntRestore routine simply time stamps the return to normal)
    Is the GOTO meant to execute only when the IF statement on the same line is true? If so I wonder if it does? I ask because to me (which does not mean it's correct, hence the question) the above looks the equivalent of
    Code:
    If B7 = 065 THEN Apt = 509
    GOTO DispAlarm1
    Meaning that the GOTO will execute unconditionally. If the GOTO should execute only when the IF statement is true I would have expected it to be
    Code:
    If B7 = 065 THEN
      Apt = 509
      GOTO DispAlarm1
    ENDIF
    Could you clarify this for me?

    /Henrik.

  2. #2
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: Changing from If..Then to Select Case

    I agree with the others - IF THEN will be less code than SELECT CASE, however a couple of LOOKUP statements will be even less. I'm not sure if the mapping ever changes, but you could even track the mapping in Excel making updates easier. This is the sort of situation that LOOKUP (or LOOKUP2) was designed for.

  3. #3
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Default Re: Changing from If..Then to Select Case

    Thanks for your replies Henrik and Charlie.

    Yes the GOTO is only executed if the If Then is true. Doing it this way eliminates the End If statement for each line. At the time I tried this several different ways and this worked out fine. This system was designed back in 2005 and has been working fine since. I just recently had a situation where the printer output stopped working and since I cleared up the problem my brain juices told me to look into potential upgrades to the system so I am going over the code for the Master and Slaves to see if I can't make this any better. The system could go for months without any alarm activations so the speed of the system going through the IF Thens is really not a problem. I am only looking at less the a couple of seconds and that is nothing in this case. The only reason I though about the Select Case is I thought I read in a code optimization article that it used less code than the If Then hence the inquiry.

    I really appreciate all of the replies and for you folks taking the time to help me. I will let you know how things work out.

    Thanks again,

    BobK

Similar Threads

  1. About Select Case
    By polymer52 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 28th February 2010, 12:54
  2. Select case...Just wondering....
    By muddy0409 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 30th December 2006, 00:23
  3. select case question
    By ronjodu in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th March 2006, 11:01
  4. Select Case syntax
    By keithdoxey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th September 2005, 20:19
  5. Select Case
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th June 2005, 20:18

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

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