5

What I see is just a simple if/then/else. What I am looking for is to be able to add multiple options, more than yes/no.

I use a motor as a switcher, so I rotate it to some position and measure its degrees to switch the mode of the robot and there are going to be more than 2 modes, like 4-8. How can I do it in an elegant way?

4 Answers 4

5

I just discovered this a few days ago. If you use "Numeric" as the input type and switch to tabbed view, you can have a select/case/default type instruction instead of if/then. It also works with text input as well as as some sensors.

enter image description here

enter image description here

2

I would suggest stacking your switch statements by placing switch statements into one another like in the below image. Make sure your conditions overlap, so you are only testing for values in A1 and A2 that are WITHIN your A condition, same for B.

Switches in a switch

1

Yes, multiple switch statements can be nested inside each other.

However, given the problem of a measuring a motor's rotation and converting that into a user choice doing the math first and using that result in a single switch statement is possible.

Also, the logic will need to run more than once and give the user some indication of the current selection. That much, might look like this:

enter image description here

0

It depends on what problem you're trying to solve. If you need a large, multi-part switch statement, then Sergei's answer above is genius and definitely correct. I just discovered that the first if counts as the default. This is different than C or Java where the default condition is optionally coded at the bottom of a switch statement. The only time nested if statements make sense is if it creates clarity within the program. I'd much rather look at one large switch block with all of the cases listed than to see it broken up with multiple if/else blocks. i.e.

if
else if
else if
else if
else

looks a lot more clear than a messy bunch of nested if/else statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.