Using switch to make decision based on number range

Hi there! New to UI path and doing practice. Hop you can help with this basic question on using Switch:

I want to give a different message based on an input number:

1-19: “Low number”
20-39: “Almost low number”
40-59: “Medium number”
60-79: “Almost high number”
80-100: “High number”

I could make them as a bunch of if-statements: "if number in range(1,19): “Low number”

But since there are five scenarios, I want to use Switch.

I save the user input number as an Int32

But how do I get the cases to accept a range of numbers instead of just one inputs number?
Probably the UserInputNumber variable could be an array instead of Int, but not sure how.

For context: see the attached screenshot of the simple sequence with 5 cases - but here each case only represents one number.

An Else If activity would be natural for this use case.

1 Like

Hi @RebeccaB

You can put the whole code in while loop it will take inputs till the if condition is met. If the if condition is met it will break the while loop. In this case if input value is 0 it will break the loop.

1 Like

Simple Approach

Rating Array (for the 100 Border we added High number again:

Checker
grafik

Mapper

Code:
arrRatings(CInt(Math.Floor(YourIntVar/20)))

1 Like

As alternates we can:

  • modify the simple Approach
  • setting up a lookUp Dictionary holding label and ranges
1 Like

Hello @RebeccaB
Welcome to the UiPath Community. Look into the Xaml file I attached below, you will get some idea.
Forum_Number_Compare.zip (46.3 KB)

1 Like

Hi @RebeccaB

the easist way

in assign activity using string variable and passing your number variable

If(number > 0 And number <=19, "Low number", 
If(number > 19 And number <= 39, "Almost low number",
If(number > 39 And number <=59, "Medium number", 
If(number > 59 And number <= 79, "Almost high number", 
If(number > 79 And number <= 100, "High number", "Invalid Number")))))

image

2 Likes

@RebeccaB

Welcome to the community

Using elif would be the best way as switch cannot take an array

You can use switch like this

In expression use If(InputNumber<=19,"Low Number",If(InputNumber<=39,"Almost Low Number,If(InputNumber<=59,Medium Number",If(InputNumber<=79,"Almost High Number","High Number"))))

And cases will be low number ,…high number

Cheers

1 Like