SPONSORS:






User Tag List

Results 1 to 6 of 6
  1. #1
    Senior Member
    Join Date
    Jul 2001
    Location
    Brookline,NH USA
    Posts
    147
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Switch Statement vs. Select Case

    From my programming experience the only difference I can establish between these two is that 'Select...Case' is used in VB where 'Switch...Case' is used in C++.

    For whatever reason, SilkTest supports them both. All the research I've been able to accomplish yields only one small difference between the two:

    - Case statements used with Select must be boolean expressions
    - The Switch statement includes any type of expression on the same line, and the associated Case statements contain your specified values

    Yet they can still do the same thing... For example:

    testcase switchselect ()

    for i = 1 to 12

    print ("Switch")
    switch (i)
    case 1 // compares i to 1
    print (i, "Case 1")
    case 2,4,6,8,10,12 // if i is an even number
    print (i, "All even cases")
    default // all other cases not explicitly described above
    print (i, "A default case")

    print ()
    print ("Select")
    select
    case i == 1 // if i is equal to 1
    print (i, "Case 1")
    case (i % 2 == 0)
    print (i, "All even cases")
    default // all other cases not explicitly described above
    print (i, "A default case")

    Is there more to it or is the decision of use based strictly on personal preference? It just seems odd to me that they would support two practically identical statements...

    ~ Fred

    ------------------

  2. #2
    Senior Member
    Join Date
    Jul 2000
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Switch Statement vs. Select Case

    I believe the support is "historical" -- the Select was around back in the early QAP days, and somewhere along the way they added the Switch (and kept Select for backward compatibility).

    I don't think it's anything more than that ... but it is just my vague recollection of the "good ol' days".

    ------------------

  3. #3
    Senior Member
    Join Date
    Aug 1999
    Location
    MA
    Posts
    129
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Switch Statement vs. Select Case

    I prefer switch, probably since I completed courses in C and C++ prior to using 4Test.

    I'd take either over code that is if / else / if / else...

    I think switch is also an excellent way to keep the code more maintainable and readable.

    ------------------
    Steven Tyler
    SQA Automation Team Manager
    Workforce Central Group
    Kronos Incorporated
    E-Mail: styler@kronos.com
    www.kronos.com
    Improving the Performance of People and Business™
    Steven Tyler
    Manager - Performance Engineering

    Kronos Incorporated
    tel: +1 978 947 4219

  4. #4
    Senior Member
    Join Date
    Jul 2001
    Location
    Brookline,NH USA
    Posts
    147
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Switch Statement vs. Select Case

    So I guess it's just personal preference then?

    ------------------

  5. #5
    Member
    Join Date
    Mar 2001
    Location
    nashville, tn, usa
    Posts
    86
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Switch Statement vs. Select Case

    I just discovered the scenario where select can be used but switch cannot.

    You have a string (sString) and it's values can be "Name", "2", or "".

    Select works...
    select
    case sString == "Name"
    case IsDigit(sString)
    case sString == ""

    Switch won't work b/c you have to maintain the same logic...
    switch sString
    case "Name"
    *checking for digit doesn't work*
    case ""

    If you happen to have a small set of numbers then the second case statement could be...
    case "1" | | "2" | | "3"

    and the switch would work.

    ------------------

  6. #6
    Senior Member
    Join Date
    Jul 2001
    Location
    Brookline,NH USA
    Posts
    147
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Switch Statement vs. Select Case

    Joe,

    Thanks for the insight. So in summary we can conclude that a switch statement can perform only one evaluation and then subsequently compare those results to any number of defined cases. And a select statement performs a boolean evaluation for each defined case.

    I wouldn't have thought of this on my own, but your post makes it quite obvious...

    ~ Fred

    ------------------

 

 

Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 12:25 PM.

Copyright BetaSoft Inc.