Can switch be used for multiple cases and can they read a list of items?

I have a set of lists in the format
Option1;Option2;Option 3
which I can convert into a string.
I would like to apply a set of instructions to execute based on which options have been selected for each list. Sometimes I will need more than one, e.g option1 and option3 would both need executing. If I have a string containing the option1;option3 text can switch be used to carry out both the case for option1 and the case for option3?
Alternatively is there a better way of doing this? Thanks

better to use if condition in this scenario

Hi, I could do individual if boxes for each scenario, is there a way to make the statement in each if box check for a different option in the string? E.g if string contains “option1” exact text then do X else skip to next if statement for option 2 and so on

Use nested Ifs or
in switch case you give the condition and in case pass the string

Don’t use nested Ifs. Use Else If.

You’ll want to use an Else If for this.

better to use switch if more than 4-5 conditions

Don’t use nested If/Else If or Switch if you want to execute them independently. Use separate If-statements. Otherwise it will only execute the first matching condition.

E.g. if your lst = {"Option1", "Option2"}

Both option1 and option2 will be executed:

If (lst.Contains("Option1")) {
    // your code
}

If (lst.Contains("Option2")) {
    // your code
}

Only Option1 will be executed:

If (lst.Contains("Option1")) {
    // your code
} Else If (lst.Contains("Option2")) {
    // your code
}
1 Like

A Switch won’t work for their requirements.

The point to Else If is it does what nested Ifs do, without nesting.

Else If doesn’t fulfill OPs requirement.

Yes, it does. Else If is basically a switch that allows expressions for the Case values.

No it doesn’t since we already confirmed that a switch doesn’t fulfill the requirements.

The Else If is only executed if the previous If condition doesn’t match. In OP’s example where the list = {“Option1”, “Option3”}, only Option1 will be executed in a Else-If-statement since it’s the first matching condition.

1 Like

You could get it to work if the conditions are formatted properly, but that could get messy depending on how many combinations there are etc.

There will be a lot of combinations. Approximately 20-30 different options I think, so a large number of possible combinations

Oh, yeah, Else If would be a nightmare. Doable but not clean.

Thinking about this some more, you could do it with a For Each. You For Each through the list (ie Option1, Option2, Option3) and the Switch is inside the For Each. Then you don’t need any complicated logic, it’ll execute each Case mentioned in the list.

This example will execute both Option1 and Option3, and skip Option2.

image

1 Like

Thanks Paul that sounds like a smart way of dealing with this. Would it need commas in between the options? Mine currently outputs with semicolons but I could potentially replace all with commas in excel first.