Random number -> Corresponding LED lit (n00besque content contained)


Results 1 to 14 of 14

Threaded View

  1. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    Select Case is a lot like a "Load of IF's" with a few differences.
    1. It removes some of the (n00besque content contained) look and feel.
    2. Only the first "CASE" that evaluates TRUE will execute, then it exits the SELECT block.
      In your list of IF's, if the first one is true, it still goes on and evaluates all the other IF's too.
      This allows more complex logic, where more than one test can be true, but only the first one is acted on.
    The same functionality using IF statements would look like this ...
    Code:
    IF MYBYTE = 1 THEN
        HIGH LED1
    ELSE
        IF MYBYTE = 2 THEN
            HIGH LED2
        ELSE
            IF MYBYTE = 3 THEN
                HIGH LED3
            ELSE
                IF MYBYTE = 4 THEN
                    HIGH LED4
                ELSE
                    IF MYBYTE = 5 THEN
                        HIGH LED5
                    ELSE
                        IF MYBYTE = 6 THEN
                            HIGH LED6
                        ENDIF
                    ENDIF
                ENDIF
            ENDIF
        ENDIF
    ENDIF
    OR, if you have PBP 2.60 you could do it like this ...
    Code:
    IF MYBYTE = 1 THEN 
        HIGH LED1
    ELSEIF MYBYTE = 2 THEN 
        HIGH LED2
    ELSEIF MYBYTE = 3 THEN 
        HIGH LED3
    ELSEIF MYBYTE = 4 THEN 
        HIGH LED4
    ELSEIF MYBYTE = 5 THEN 
        HIGH LED5
    ELSEIF MYBYTE = 6 THEN 
        HIGH LED6
    ENDIF
    It may not seem like much difference with this simple example, but with large SELECT CASE blocks using more complex comparisons, it can save a boat load of execution time not having to evaluate every condition.

    * SELECT CASE is also much easier to read. Especially when it's not your program.

    PicBasic Pro, was written to generate the Smallest and Fastest code possible. As it is the HIGH/LOW statements only use a couple of instruction cycles. If you add in the abilities to use them like array's and access pins non-contiguously, it would make EVERY HIGH/LOW statement Bigger and Slower, breaking everyone's code, and causing more complaints than not having those abilities produces.
    Last edited by Darrel Taylor; - 23rd April 2011 at 16:29.
    DT

Members who have read this thread : 0

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