Conditional logic is most commonly implemented through the use of an if-then-else statement. However, when an RPA developer is faced with multi-branched logic based on a single condition, an if activity can become unwieldly, especially if the nesting of if statements goes beyond a second level. In these situations, a better option is to use a conditional operator that supports multiple branches. That is what we will explore in this UiPath Switch activity example.
To use a UiPath Switch activity in an RPA project, follow these steps:
- Add the UiPath Switch activity from the Activities tab.
- Set a variable to act as the switch’s expression.
- Add a case for each possible expression match.
- Add an optional default if there is no switch case that matches.
- Implement the appropriate logic for each case.
|A strategy to learn UiPath Orchestrator|
|There are five phases involved in order to create, deploy and manage RPA robots at runtime:
String as a UiPath Switch expression
You will notice that the expression used in the UiPath Switch example defaults to an int32. This can be changed to a String, boolean value or any other reference type by configuring the expression type in the switch activity’s properties panel.
This UiPath Switch example follows through a hypothetical situation where an int32 variable named players is passed to the activity’s expression. Depending on how many players are available, the application tells the user the best matching sport to play:
- With 11 players, there is enough for a cricket team.
- With 15 players, a hurling team could be assembled.
- With 6 players, a hockey team could be formed.
- If there is no match, the user is told to take everyone surfing.
UiPath Switch vs. If
As you can see, this UiPath Switch example implements the required logic in a clean and methodical way. If this was to be implemented with a conditional UiPath If activity in UiPath Studio, the code would be messy, visually unappealing and difficult to maintain and troubleshoot. When the situation demands it, the UiPath Switch activity is an excellent way to implement conditional logic.