Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape, CAD, maker project and design.

First time visiting? Here are some places to start:

  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Switch statement

john_mcclaryjohn_mcclary Member, Developers Posts: 1,217 PRO
Is there an equivalent way of doing switch statements in FS?

instead of a bunch of if-else statements

Best Answers

  • ilya_baranilya_baran Posts: 842
    Accepted Answer
    There are indeed cases when a switch statement makes things simpler, but in this case, you could get a similar effect by doing:
    const table = {
     ...
       3/4 : { KeySeat : .644, KeyWay : .837},
     13/16 : { KeySeat : .708, KeyWay : .900},
     ...
    };
    var entry = table[ShaftDia];
    if (entry == undefined)
       throw "No match for shaft diameter: " ~ ShaftDia;
    var KeySeat = entry.KeySeat;
    var KeyWay = entry.KeyWay;

    Ilya Baran \ Director of FeatureScript \ Onshape Inc

Answers

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,055
    @john_mcclary @konstantin_shiriazdanov

    @ilya_baran informs me that there is a switch-expression in FS that we may document for public use in the future.  It is slightly different from a switch-statement in that it does not switch on an expression and then execute a block of code (a statement), but rather switches on an expression and then return another specified expression.  This can be made to act like a traditional switch statement using lambdas if desired.

    Here is an example:
    https://cad.onshape.com/documents/f6780441494046f4439a8219/w/71e7c5c95c521d8716fa75fa/e/631535138fdaba3ef7928ea2
    Jake Rosenfeld - Modeling Team
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 739 ✭✭✭✭
    @Jake_Rosenfeld so this switch is a function, I beleve Ilya wrote it half an hour ago)
  • john_mcclaryjohn_mcclary Member, Developers Posts: 1,217 PRO
    edited August 27
    @Jake_Rosenfeld well I suppose that is similar to a switch statement, but it doesn't allow flow through multiple cases, and it still requires many lines of code.

    y = 3;
    switch(x)
    {
    case 0: y++;
    case 1: y++;
    case 2: y++; break;
    case 3: y = 10; break;
    default: y = 0;
    }

    X = 0 -> Y = 6;
    X = 1 -> Y = 5;
    X = 2 -> Y = 4;
    X = 3 -> Y = 10;
    X = 4 -> Y = 0;

    Not that I end up using that method very often, but it is a traditional syntax for switch which I have taken advantage of before.

    What brought this on was I had a long list of if-else statements to fill in all the keyway sizes given a shaft diameter.
    it ended up being 500 lines of code that is ugly to read.
    if they were switch statements it would only be around 100 or so lines of code and resemble a readable table.



    Vrs:

    case 7/8: KeySeat = .771; KeyWay = .900; break;
    case 15/16: KeySeat = .796; KeyWay = 1.051; break;
    case 1: KeySeat = .859; KeyWay = 1.114; break;
  • ilya_baranilya_baran Onshape Employees, Developers Posts: 842
    Accepted Answer
    There are indeed cases when a switch statement makes things simpler, but in this case, you could get a similar effect by doing:
    const table = {
     ...
       3/4 : { KeySeat : .644, KeyWay : .837},
     13/16 : { KeySeat : .708, KeyWay : .900},
     ...
    };
    var entry = table[ShaftDia];
    if (entry == undefined)
       throw "No match for shaft diameter: " ~ ShaftDia;
    var KeySeat = entry.KeySeat;
    var KeyWay = entry.KeyWay;

    Ilya Baran \ Director of FeatureScript \ Onshape Inc
  • john_mcclaryjohn_mcclary Member, Developers Posts: 1,217 PRO
    Thanks @ilya_baran ; That's what I'm looking for for this use case
  • john_mcclaryjohn_mcclary Member, Developers Posts: 1,217 PRO
    Went from 465 lines of code down to 189 with that

    I'll know better next time :)
  • mbartlett21mbartlett21 Member Posts: 1,032 EDU
    @Jake_Rosenfeld
    Can you specify a default with that switch statement?
  • john_mcclaryjohn_mcclary Member, Developers Posts: 1,217 PRO
    think that is what the if(entry == undefined) is doing
Sign In or Register to comment.