Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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_mcclary
Member, Developers Posts: 3,938 PRO
Is there an equivalent way of doing switch statements in FS?
instead of a bunch of if-else statements
instead of a bunch of if-else statements
2
Best Answers
-
konstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭I would happily use "switch" too if it was there.
6 -
ilya_baran Onshape Employees, Developers, HDM Posts: 1,215There are indeed cases when a switch statement makes things simpler, but in this case, you could get a similar effect by doing:
const table = {<br> ...<br> 3/4 : { KeySeat : .644, KeyWay : .837},<br> 13/16 : { KeySeat : .708, KeyWay : .900},<br> ...<br>};<br>var entry = table[ShaftDia];<br>if (entry == undefined)<br> throw "No match for shaft diameter: " ~ ShaftDia;<br>var KeySeat = entry.KeySeat;<br>var KeyWay = entry.KeyWay;<br>
Ilya Baran \ VP, Architecture and FeatureScript \ Onshape Inc1 -
Jake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646@mbartlett21
The switch expression does not support a default, but you can check if none of the cases were hit by checking if the switch returned `undefined`, and then doing whatever default behavior you want (similar to what Ilya does above in his map example).Jake Rosenfeld - Modeling Team5
Answers
@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
y = 3;<br>switch(x)<br>{<br>case 0: y++;<br>case 1: y++;<br>case 2: y++; break;<br>case 3: y = 10; break;<br>default: y = 0;<br>}<br><br>
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;
I'll know better next time
Can you specify a default with that switch statement?
IR for AS/NZS 1100
The switch expression does not support a default, but you can check if none of the cases were hit by checking if the switch returned `undefined`, and then doing whatever default behavior you want (similar to what Ilya does above in his map example).
Switch no longer seems to work unfortunitally, look forward to seeing it in the future tho
@Chris_D_Mentes_001
What do you mean? Onshape uses it many times in their own code and we can create code just like it:
IR for AS/NZS 1100
Ah okay thanks for this I've been using it wrong.