# Optimizing code for conditional "if" assignments

**URL:** <https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604>\
**Category:** Help\
**Tags:** question, studio, activities, system\
**Created:** [November 14, 2024, 11:31pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604 "2024-11-14T23:31:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matias\_Clemente.Arg](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/matias_clemente.arg/32/497209_2.png) [@Matias\_Clemente.Arg](https://forum.uipath.com/u/Matias_Clemente.Arg)\
**Post date:** [November 14, 2024, 11:31pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/1 "2024-11-14T23:31:14Z")

</div>

Is there a way to simplify this code? The system of boxes for a simple “if this condition is met, assign a value” seems overly cumbersome to me.

 ![The image shows a flowchart used in an automation process, featuring a condition that checks if the output string equals "INGRESOS TOTALES" and, if true, assigns the value "Ingresos" to the variable "banderaIngresosTotales". (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/7/7/c/77c6929947e27d114d5379cf9418f151dd2972cc.png)

---

<div class="post-metadata">

**Author:** ![Yoichi](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/yoichi/32/523754_2.png) [@Yoichi](https://forum.uipath.com/u/Yoichi)\
**Post date:** [November 14, 2024, 11:47pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/2 "2024-11-14T23:47:20Z")

</div>

Hi,

How about using Regex.Replace?

```
newString = System.Text.RegularExpressions.Regex.Replace(yourString,"^INGRESOS TOTALSE$","Ingresos")

```

Regards,

---

<div class="post-metadata">

**Author:** ![Matias\_Clemente.Arg](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/matias_clemente.arg/32/497209_2.png) [@Matias\_Clemente.Arg](https://forum.uipath.com/u/Matias_Clemente.Arg)\
**Post date:** [November 15, 2024, 12:17am UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/3 "2024-11-15T00:17:18Z")

</div>

I do not need to replace the values. It is a series of “if” statements nested within other “if” statements where the “else” part is not used. The code becomes very difficult to read.

 ![The image shows a flowchart in a software environment with conditional logic blocks that assign variables based on whether the output string is "INGRESOS TOTALES" or "EGRESOS TOTALES". (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/d/9/5/d95c72b7842aaebc92e286ae931527ef1d6474ed.png)  
 ![This image depicts a flowchart from a programming environment showcasing conditional logic for checking cell values, controlling flags, and saving totals in specific variables. (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/6/8/b/68b033217ad59fc6277afd13855aa7425a3ab4ca.png)

---

<div class="post-metadata">

**Author:** ![Yoichi](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/yoichi/32/523754_2.png) [@Yoichi](https://forum.uipath.com/u/Yoichi)\
**Post date:** [November 15, 2024, 12:29am UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/4 "2024-11-15T00:29:01Z")

</div>

Hi,

Because the above can assign to replaced string to other variable, it can replace IF activity.

Of it’s necessary to convert a lot of pairs of keyword, the following approach using dictionary will help you.

 ![image](https://global.discourse-cdn.com/uipath/original/4X/5/4/7/5479aed6716612d22f42cef9f001f7ba6189fcb6.png)

```
dict = New Dictionary(Of String,String)From{{"INGRESOS TOTALSE","Ingresos"},{"From","To"}}

```

Next check if it has input string (let’s say yourString) as key. Then we can use `dict(yourString)`

Regards,

---

<div class="post-metadata">

**Author:** ![Matias\_Clemente.Arg](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/matias_clemente.arg/32/497209_2.png) [@Matias\_Clemente.Arg](https://forum.uipath.com/u/Matias_Clemente.Arg)\
**Post date:** [November 16, 2024, 1:13am UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/5 "2024-11-16T01:13:19Z")

</div>

Hello, to be honest, I did not understand your solution. I am attaching a pseudocode to clarify what I need. The number of ‘if’ statements makes the workflow very difficult to read, as the IF activity does not allow the removal of the ‘else’, nor does it permit the inclusion of two conditions within it.

for x= 1 to 20  
if cell = “Ingresos” then  
Flag = 1  
end if  
if cell = Total" then  
if flag= 1 then  
value= “X”  
flag = 2  
end if  
if flag = 2 then  
value = “Y”  
end if  
end if  
next

---

<div class="post-metadata">

**Author:** ![Anil\_G](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/anil_g/32/524291_2.png) [@Anil\_G](https://forum.uipath.com/u/Anil_G)\
**Post date:** [November 16, 2024, 6:16am UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/6 "2024-11-16T06:16:16Z")

</div>

@Matias_Clemente.Arg

is this what you are looking for…to make it less clutter

 ![The image depicts a flowchart with nested conditional statements and variable assignments designed to handle different conditions based on the values of "str" and "flag" within a loop ranging from 1 to 20. (Captioned by AI)](https://global.discourse-cdn.com/uipath/original/4X/1/2/0/12055da3e4ebf761c3cef33439fd201088b7c0cc.png)

this is else if activity

cheers

---

<div class="post-metadata">

**Author:** ![SorenB](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sorenb/32/500790_2.png) [@SorenB](https://forum.uipath.com/u/SorenB)\
**Post date:** [November 16, 2024, 8:19am UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/7 "2024-11-16T08:19:39Z")

</div>

Hello @Matias_Clemente.Arg

Perhaps you could use a **Switch** activity to make the design canvas more sleek to look at, in some of your cases.

Assuming that some of the cases could only have one outcome, the switch can have several cases, but only select one of them.

```auto
Switch expressions = salida.ToString
Case "INGRESOS TOTALES"
Do something...
Case "EGRESOS TOTALES"
Do something else...

```

Regards  
Soren

---

<div class="post-metadata">

**Author:** ![sudster](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sudster/32/503982_2.png) [@sudster](https://forum.uipath.com/u/sudster)\
**Post date:** [November 16, 2024, 11:47am UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/8 "2024-11-16T11:47:23Z")

</div>

@Anil_G , please correct me if I am wrong…

My understanding is that, in an ElseIf activity, if a condition evaluates true, all other conditions will be skipped for that iteration, which means flag=1 and flag=2 will never be true?

I haven’t actually tested my theory though 😀

---

<div class="post-metadata">

**Author:** ![sudster](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sudster/32/503982_2.png) [@sudster](https://forum.uipath.com/u/sudster)\
**Post date:** [November 16, 2024, 12:07pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/9 "2024-11-16T12:07:30Z")

</div>

@Matias_Clemente.Arg ,

> [@Matias\_Clemente.Arg](#):
>
> for x= 1 to 20  
> if cell = “Ingresos” then  
> Flag = 1  
> end if  
> if cell = Total" then  
> if flag= 1 then

From a quick look, with the way you’ve structured the logic, flag will never be 1 when cell = Total. This is because flag = 1 when the cell = Ingresos

I often resort to using a quick Flowchart rather than drowning in recursive if…else 😀

---

<div class="post-metadata">

**Author:** ![Anil\_G](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/anil_g/32/524291_2.png) [@Anil\_G](https://forum.uipath.com/u/Anil_G)\
**Post date:** [November 16, 2024, 1:31pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/10 "2024-11-16T13:31:32Z")

</div>

@sudster

Thats true…but if you see the scenario above they are dependant…values for same variable are different in each condition

Flag will be 1 for the following iteration and not the current

Cheers

---

<div class="post-metadata">

**Author:** ![Matias\_Clemente.Arg](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/matias_clemente.arg/32/497209_2.png) [@Matias\_Clemente.Arg](https://forum.uipath.com/u/Matias_Clemente.Arg)\
**Post date:** [November 16, 2024, 2:00pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/11 "2024-11-16T14:00:38Z")

</div>

Perhaps I wrote the pseudocode incorrectly, but the real code does work. However, the purpose of the question was to find out if there was a way to simplify when multiple if statements appear in succession, as it is very annoying to correct or follow a workflow when that happens. It is not very readable.

---

<div class="post-metadata">

**Author:** ![J0ska](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/j0ska/32/111328_2.png) [@J0ska](https://forum.uipath.com/u/J0ska)\
**Post date:** [November 16, 2024, 2:07pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/12 "2024-11-16T14:07:30Z")

</div>

This is the dark side of low code ☹  
You could use coded workflow instead.  
Cheers

---

<div class="post-metadata">

**Author:** ![sudster](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sudster/32/503982_2.png) [@sudster](https://forum.uipath.com/u/sudster)\
**Post date:** [November 16, 2024, 2:13pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/13 "2024-11-16T14:13:32Z")

</div>

> [@Matias\_Clemente.Arg](#):
>
> if there was a way to simplify when multiple if statements appear in succession

You can just use an Assign activity and write out the if statement.

```auto
var = If(x = y, true, false)

```

---

<div class="post-metadata">

**Author:** ![sudster](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sudster/32/503982_2.png) [@sudster](https://forum.uipath.com/u/sudster)\
**Post date:** [November 16, 2024, 2:19pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/14 "2024-11-16T14:19:49Z")

</div>

> [@Anil\_G](#):
>
> Flag will be 1 for the following iteration and not the current

Yes, that is possible. But I don’t see the flag variable initialised outside the for loop. Anyway, there are multiple ways to skin a cat!

---

<div class="post-metadata">

**Author:** ![Anil\_G](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/anil_g/32/524291_2.png) [@Anil\_G](https://forum.uipath.com/u/Anil_G)\
**Post date:** [November 16, 2024, 2:53pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/15 "2024-11-16T14:53:26Z")

</div>

@sudster

Agreed…

But in this case…say in first iteration value is ingress the flag changes to 1…

Then it can be a total and 1 combination as well…

Cheers

---

<div class="post-metadata">

**Author:** ![Matias\_Clemente.Arg](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/matias_clemente.arg/32/497209_2.png) [@Matias\_Clemente.Arg](https://forum.uipath.com/u/Matias_Clemente.Arg)\
**Post date:** [November 16, 2024, 4:52pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/16 "2024-11-16T16:52:09Z")

</div>

Hello, thank you all for your responses. However, the purpose of the question was to find a way to simplify the code “visually.” The code works, but the chaining of IF statements in UiPath seems unfriendly to me because it does not allow the removal of “Else” clauses. Perhaps, as @J0ska mentioned, that is the way to go. But it will have to wait for another time, when I have regained energy to tackle that topic.

---

<div class="post-metadata">

**Author:** ![J0ska](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/j0ska/32/111328_2.png) [@J0ska](https://forum.uipath.com/u/J0ska)\
**Post date:** [November 16, 2024, 5:24pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/17 "2024-11-16T17:24:10Z")

</div>

> [@Matias\_Clemente.Arg](#):
>
> UiPath seems unfriendly

Have you seen Microsoft PowerAutomate? This is unfriendly 😃

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [November 19, 2024, 5:25pm UTC](https://forum.uipath.com/t/optimizing-code-for-conditional-if-assignments/779604/18 "2024-11-19T17:25:07Z")

</div>

Closed on November 19, 2024, 5:25pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
