Announcement

Collapse
No announcement yet.

Switch Function

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

  • Switch Function

    Please add your comments and suggestions as a Reply to this thread.





    PB/CC - SWITCH function

    Purpose
    Return one of a series of values based upon a TRUE/FALSE evaluation of a corresponding series of expressions.
    Syntax
    var = SWITCH(expr1, val1 [[, expr2, val2], ...])
    var& = SWITCH&(expr1, val1& [[, expr2, val2&], ...])
    var$ = SWITCH$(expr1, val1$ [[, expr2, val2$], ...])
    Remarks
    SWITCH expects values of any numeric type. SWITCH& expects values optimized for Long-integer type. SWITCH$ expects values of string type.

    If expr1 evaluates TRUE, val1 is returned, if expr2 evaluates TRUE, val2 is returned, etc.

    Each control expression in the series is evaluated as a typical PowerBASIC Boolean expression, which offers short-circuit expression evaluation as needed. To force a bitwise evaluation of an expression, enclose it in parentheses. The value parameters may be expressions, literals or variables of the appropriate data type for the SWITCH function in use.

    SWITCH returns the matching value parameter from the first TRUE evaluation of the control expressions, evaluated from left to right in the list. Therefore, it would be wise to place the most likely selections at the front of the SWITCH list to achieve the utmost efficiency. If no expressions evaluate to TRUE, then zero (0) is returned.
    Restrictions
    Contrary to the implementation in some other languages, only the chosen value (one of val1, val2, val3…) is evaluated at run-time; the other value parameters are not. This ensures optimum execution speed, as well as the elimination of unanticipated side effects.
    See AlsoReferencesExamples
    Code:
    ' SWITCH with simple expressions
    A$ = SWITCH$(x%=1, "Bob", x%=20, "Bruce", x% > 20, "Dan", x% < 1, "Nobody!")
    ' SWITCH with complex expressions
    FUNCTION z(i&) AS LONG
      INCR i&
      FUNCTION = i&
    END FUNCTION
    FUNCTION PBMAIN
      x& = -1
      Choice& = SWITCH&(z(x&), 1, z(x&), 2, z(x&), 3)
      ' Choice& will equal 2
    END FUNCTION
    Last edited by Gary Beene; 29 Oct 2014, 05:49 AM.

  • #2
    Does anyone use SWITCH ?

    While the help file describes it quite well I cannot get my head around a real world scenario or use in production code.

    James

    Comment


    • #3
      Having done a search of the fora for 'switch', there doesn't seem to be much use of SWITCH. It could be that it we pass it over since we're not accustomed to it. We use it a lot in comments though!
      Rod
      "To every unsung hero in the universe
      To those who roam the skies and those who roam the earth
      To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

      Comment


      • #4
        Code:
           Z = SWITCH (condition1, value1, condition2, value2,  [..,conditionN, valueN))
        =
        Code:
            IF condition1 THEN 
                Z= value1
           ELSEIF condition2 THEN 
                Z = value2 
           ....
            ELSEIF  conditionN ....   THEN 
                 Z = valueN
            END IF
        MCM
        Last edited by Michael Mattias; 22 Sep 2015, 09:40 AM. Reason: Remove default value processing (not part of SWITCH function)

        Comment


        • #5
          MCM,
          I believe you are mistaken.
          There is no default with SWITCH as there is with CHOOSE

          James

          Comment


          • #6
            There is no default with SWITCH as there is with CHOOSE
            You're right! I missed that!
            Michael's right about it being similar to a IF/THEN/ELSE (block or single line) or even a SELECT CASE block.
            The documentation doesn't state a maximum number of expression/value pairs so I assume, until tested, that line length is the limitation.
            Rod
            "To every unsung hero in the universe
            To those who roam the skies and those who roam the earth
            To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

            Comment


            • #7
              There is no default with SWITCH as there is with CHOOSE
              My bad. Corrected above.
              ... about it being similar to a IF/THEN/ELSE (block or single line) or even a SELECT CASE block....
              Not like SELECT CASE; SELECT CASE tests ONE expression for multiple values and SWITCH tests multiple expressions for individual truth.

              Although I 'suppose' you could use something like...

              Code:
               Z= SWITCH (X=1, A, X=2, B, X=3, C)
              .... where SWITCH produces the same result as would the more natural 'SELECT CASE X ' ; but using SWITCH here you are actually testing multiple conditions for truth.
              Last edited by Michael Mattias; 22 Sep 2015, 09:43 AM.

              Comment


              • #8
                Well, whether it acts like an IF/THEN/ELSE or a SELECT CASE block or not, it does act like a SWITCH function.
                Code:
                #COMPILE EXE
                #DIM ALL
                
                FUNCTION PBMAIN () AS LONG
                  LOCAL gWin AS LONG
                  LOCAL result, ndx, ex1, ex2 AS LONG
                  GRAPHIC WINDOW NEW "Switcheroo", 100,100,200,250 TO gWin
                  FOR ex1=1 TO 3
                    FOR ndx=1 TO 5
                      INCR ex2
                      result=SWITCH(ndx>4 AND ex1<ndx,ndx*ex1, ex1<ndx,2, ex1=ndx, 3, ndx=1 OR ndx<ex1,4)
                      GRAPHIC PRINT ex2 result
                    NEXT ndx
                  GRAPHIC PRINT
                  NEXT ex1
                  GRAPHIC WAITKEY$
                
                  GRAPHIC WINDOW END
                END FUNCTION
                Note the expression in the value position in the first pairing.
                Rod
                "To every unsung hero in the universe
                To those who roam the skies and those who roam the earth
                To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

                Comment

                Working...
                X