Announcement

Collapse
No announcement yet.

Select Case with AND ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scott Slater
    replied
    The answer (according to PB DOS manual) would be more or less NO.

    from PB DOS 3.5 Manual:
    The test which may be performed by a CASE clause include equality, in equality, greater than, less than, and range("from-to") testing. Examples of CASE clause tests include:

    CASE > b
    CASE 14
    CASE b TO 99
    CASE 14, b
    CASE 25 TO 99, 14

    When a CASE clause contains multiple tests separated by commas, a logical OR is performed. That is, if any one (or more) of the tests is TRUE, the entire clause is TRUE.
    If you want multiple AND tests, it looks like you'd have to resort to the IF BLOCK.

    Leave a comment:


  • Michael Mattias
    replied
    >is the acceptable way on the DOS version for a range test

    And so it it is...except the original question was not about a 'range'... it was..
    Can I make a SELECT CASE statement using the logical AND?? I.e., can I do this:?
    Unfortunately, the "this" demonstrated a set of multiple AND-connected conditions which just happened to be re-arrangeable into a range test.

    That much-harped-upon need for 'communications skills' just keeps turning up everywhere, doesn't it?

    MCM

    Leave a comment:


  • Scott Slater
    replied
    To answer the original question you could use;

    Code:
    Dim x As Integer
    
    For x = 1 To 26   ' for loop only added to show how each result is handled.
    
       Select Case x
       Case 6 To 11, 18 To 24, 26
                   ? "case 1"
            Case Else
                    ? "case else"
       End Select
    
    Next x
    is the acceptable way on the DOS version for a range test. opc1:
    Last edited by Scott Slater; 30 Jun 2009, 04:29 PM. Reason: fixed code

    Leave a comment:


  • Michael Mattias
    replied
    I hate to interrupt this lovely (that's with an "oh" not an "eye") conversation, but did anyone using PB-DOS actually try the "SELECT CASE -1%" (changed to integer) construct to handle the 'compound conditions' which was, after all, the original question?

    Leave a comment:


  • John Gleason
    replied
    Originally posted by Scott Slater View Post
    Is that faster than;
    Code:
     
    ! and x, 1
    ?
    It is faster, but !test doesn't save the result in x like !and does, it just sets the flags and you would almost immediately need to !jz short label or similar based on the result.

    Leave a comment:


  • Scott Slater
    replied
    Code:
    !test x, 1
    Is that faster than;
    Code:
     
    ! and x, 1
    ?

    Leave a comment:


  • John Gleason
    replied
    Code:
    !test x, 1

    Leave a comment:


  • Scott Slater
    replied
    Originally posted by Michael Mattias View Post
    IsOdd = ISTRUE (X MOD 2)
    IsOdd = (X AND 1)

    Still faster because it translates better to assembly and does not have to do a divide.

    Leave a comment:


  • Michael Mattias
    replied
    IsOdd = ISTRUE (X MOD 2)

    Leave a comment:


  • Scott Slater
    replied
    no problem... you must have been up too late the night before writing code.

    Leave a comment:


  • Eric Pearson
    replied
    I don't drink coffee, but Yes, I posted that before my first Coca-Cola of the day. It seemed so logical at the time.

    Scott, please disregard my response.

    Leave a comment:


  • Paul Dixon
    replied
    Eric,
    did you post that before you had your first cup of coffee of the day?

    Paul.

    Leave a comment:


  • Eric Pearson
    replied
    Scott --

    Not if the number is negative. For example an INTEGER with "all bits on" is -32,768.

    -- Eric

    Leave a comment:


  • Scott Slater
    replied
    Originally posted by Erich Schulman View Post
    You can do your even/odd test with a single IF. Check the MOD 2 for true (odd) or false (even). But the SELECT CASE does convey more clearly what you intend to accomplish.
    You could AND it with 1 to make it really fast...

    Code:
    DIM X AS INTEGER
    
    If (X AND 1) Then
       Print "ODD"
    Else
       Print "EVEN"
    End If
    That simply checks BIT 1 which when ON always causes the value to be odd.

    Leave a comment:


  • Erich Schulman
    replied
    For your SELECT CASE X, you won't really lose anything much in performance or clarity if you just used a series of IF / END IF blocks. You should be able to see all of it on the screen at once. But if you anticipate adding more criteria and/or action per criterion, then the SELECT CASE would become desirable.

    You can do your even/odd test with a single IF. Check the MOD 2 for true (odd) or false (even). But the SELECT CASE does convey more clearly what you intend to accomplish.

    Leave a comment:


  • Manuel Valdes
    replied
    Though Jose Roca's answer went straight to the point, I would like to recall that SELECT CASE is more aimed to improve the code clarity and efficiency. The good old IF, ELSEIF,......, ELSEIF, ELSE, END IF can do the same, and also handle a set of far more complex logical statements.

    Leave a comment:


  • Bern Ertl
    replied


    Oops... Sorry about that.

    Leave a comment:


  • Michael Mattias
    replied
    Guess that shows you how often Bern and I use the MS-DOS compiler, huh?

    Best guess for me is not for about five years, but no new development for ten.

    Leave a comment:


  • Scott Slater
    replied
    Select Case Long is a "Windows" thing.

    Leave a comment:


  • Michael Mattias
    replied
    Code:
    FUNCTION PBMAIN () AS LONG
    
      LOCAL I AS LONG
          FOR I = -51& TO 150 STEP 50
              CALL TestSelect (I)
          NEXT
          EXIT FUNCTION
    
    END FUNCTION
    
    FUNCTION TestSelect(Z AS LONG ) AS LONG
        
        
        SELECT CASE -1&    ' true
             CASE  (Z>0) AND (Z<70)
                 MSGBOX USING$ ("# between 1 and 69", Z)
             CASE (Z>=70)
                 MSGBOX USING$ ("# >=70", Z)
             CASE (Z=-1&)
                 MSGBOX USING$("#  = minus one", Z)
             CASE ELSE
                  MSGBOX FORMAT$(Z, "#;-#")  & " is else"
                  
        END SELECT
    END FUNCTION
    (Oops, that's windows code. Well, it works with the Windows compilers. Give it a shot with the DOS compiler)

    Leave a comment:

Working...
X