Announcement

Collapse
No announcement yet.

Select Case with AND ?

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

  • Select Case with AND ?

    I am aware that I can do this:

    I am going to be a plain about this for clarity.
    (Assume Grade = 8)

    I know I can do this:
    Select Case Grade
    Case 2, 4, 6, 8
    Res$="EVEN"
    Case 1, 3, 5, 7
    Res$="ODD"
    End select
    Print Res$
    End


    That is, when a CASE clause contains multiple tests separated by commas, the logical OR is performed. Question: Can I make a SELECT CASE statement using the logical AND?? I.e., can I do this:?
    Assume X = 75

    Select Case X
    Case >70, AND <100
    Res$="PASS! Congratulations!!
    Case >0, AND < 71
    Res$="FLUNKO! Go to teacher for punishment!"
    Case < 1
    Res$="Report to office. You are being sent back a grade."
    End Select
    Print Res$


    Is that possible? :confused2:

    Thank you.

    Robert

  • #2
    No with that syntax, but you can use:

    Code:
       Select Case X
    
        Case 71 TO 99
    
            Res$="PASS! Congratulations!!
    
        Case 1 TO 70
    
            Res$="FLUNKO! Go to teacher for punishment!"
    
        Case < 1
    
            Res$="Report to office. You are being sent back a grade."
    
       End Select
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      If X / Grade are LONG variables, you should use SELECT CASE AS LONG ...
      Bernard Ertl
      InterPlan Systems

      Comment


      • #4
        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)
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Select Case Long is a "Windows" thing.
          Scott Slater
          Summit Computer Networks, Inc.
          www.summitcn.com

          Comment


          • #6
            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.
            Michael Mattias
            Tal Systems Inc. (retired)
            Racine WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              :laugh:

              Oops... Sorry about that.
              Bernard Ertl
              InterPlan Systems

              Comment


              • #8
                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.

                Comment


                • #9
                  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.
                  Erich Schulman (KT4VOL/KTN4CA)
                  Go Big Orange

                  Comment


                  • #10
                    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.
                    Scott Slater
                    Summit Computer Networks, Inc.
                    www.summitcn.com

                    Comment


                    • #11
                      Scott --

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

                      -- Eric
                      "Not my circus, not my monkeys."

                      Comment


                      • #12
                        Eric,
                        did you post that before you had your first cup of coffee of the day?

                        Paul.

                        Comment


                        • #13
                          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 :stupid: response.
                          "Not my circus, not my monkeys."

                          Comment


                          • #14
                            no problem... you must have been up too late the night before writing code. :coffee3:
                            Scott Slater
                            Summit Computer Networks, Inc.
                            www.summitcn.com

                            Comment


                            • #15
                              IsOdd = ISTRUE (X MOD 2)
                              Michael Mattias
                              Tal Systems Inc. (retired)
                              Racine WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                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.
                                Scott Slater
                                Summit Computer Networks, Inc.
                                www.summitcn.com

                                Comment


                                • #17
                                  Code:
                                  !test x, 1

                                  Comment


                                  • #18
                                    Code:
                                    !test x, 1
                                    Is that faster than;
                                    Code:
                                     
                                    ! and x, 1
                                    ?
                                    Scott Slater
                                    Summit Computer Networks, Inc.
                                    www.summitcn.com

                                    Comment


                                    • #19
                                      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.

                                      Comment


                                      • #20
                                        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?
                                        Michael Mattias
                                        Tal Systems Inc. (retired)
                                        Racine WI USA
                                        [email protected]
                                        http://www.talsystems.com

                                        Comment

                                        Working...
                                        X