# Optimize the code using assign?

**URL:** <https://forum.uipath.com/t/optimize-the-code-using-assign/757320>\
**Category:** Help\
**Tags:** activities-panel, question, studio, activities, system\
**Created:** [July 31, 2024, 2:41pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320 "2024-07-31T14:41:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob\_lavelle](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/rob_lavelle/32/401432_2.png) [@rob\_lavelle](https://forum.uipath.com/u/rob_lavelle)\
**Post date:** [July 31, 2024, 2:41pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/1 "2024-07-31T14:41:40Z")

</div>

can we write a if condition in assign activity rather than using if activity true and assign this value and flase assign the default value so that we can optimise the code ?

---

<div class="post-metadata">

**Author:** ![sandyarpa767](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sandyarpa767](https://forum.uipath.com/u/sandyarpa767)\
**Post date:** [July 31, 2024, 2:43pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/2 "2024-07-31T14:43:07Z")

</div>

Yes we can achieve the same by using if or nested conditions in assign

---

<div class="post-metadata">

**Author:** ![rob\_lavelle](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/rob_lavelle/32/401432_2.png) [@rob\_lavelle](https://forum.uipath.com/u/rob_lavelle)\
**Post date:** [July 31, 2024, 2:43pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/3 "2024-07-31T14:43:25Z")

</div>

can u just show a example ?

---

<div class="post-metadata">

**Author:** ![mkankatala](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/mkankatala/32/484097_2.png) [@mkankatala](https://forum.uipath.com/u/mkankatala)\
**Post date:** [July 31, 2024, 2:44pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/4 "2024-07-31T14:44:19Z")

</div>

Hi @rob_lavelle

Yes you can write the If condition syntax in assign activity, check below expression,

```auto
- Assign -> Output = If(Condition, "True value", "False value")

```

Hope it helps!!

---

<div class="post-metadata">

**Author:** ![sandyarpa767](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sandyarpa767](https://forum.uipath.com/u/sandyarpa767)\
**Post date:** [July 31, 2024, 2:45pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/5 "2024-07-31T14:45:01Z")

</div>

yes @rob_lavelle ,

refer below example for ref

1. **Create Variables:**

- `inputValue` (e.g., of type `Int32`)
- `result` (e.g., of type `String`)

1. **Assign Activity:**

```auto
Assign: result = If(inputValue > 10, "Greater than 10", "10 or less")

```

In this example, if `inputValue` is greater than 10, `result` will be assigned the string “Greater than 10”. Otherwise, it will be assigned “10 or less”.

Regards  
Sandy

---

<div class="post-metadata">

**Author:** ![rlgandu](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/rlgandu/32/488436_2.png) [@rlgandu](https://forum.uipath.com/u/rlgandu)\
**Post date:** [July 31, 2024, 2:45pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/6 "2024-07-31T14:45:10Z")

</div>

@rob_lavelle

```auto
result = condition ? valueIfTrue : valueIfFalse

```

---

<div class="post-metadata">

**Author:** ![rob\_lavelle](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/rob_lavelle/32/401432_2.png) [@rob\_lavelle](https://forum.uipath.com/u/rob_lavelle)\
**Post date:** [July 31, 2024, 2:45pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/7 "2024-07-31T14:45:52Z")

</div>

can we also write nested conditions in this ?

---

<div class="post-metadata">

**Author:** ![mkankatala](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/mkankatala/32/484097_2.png) [@mkankatala](https://forum.uipath.com/u/mkankatala)\
**Post date:** [July 31, 2024, 2:46pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/8 "2024-07-31T14:46:46Z")

</div>

For example… @rob_lavelle

```auto
- Assign -> Input = 60
- Assign -> Input = If(Input<50, 100, Input)

```

The condition is false Input value is not less than 50 then the Input variable again stores the same value.

Hope you understand!!

---

<div class="post-metadata">

**Author:** ![sandyarpa767](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@sandyarpa767](https://forum.uipath.com/u/sandyarpa767)\
**Post date:** [July 31, 2024, 2:46pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/9 "2024-07-31T14:46:53Z")

</div>

yes we can do ,

Assign: grade = If(score \>= 90, “A”, If(score \>= 80, “B”, If(score \>= 70, “C”, If(score \>= 60, “D”, “F”))))  
Regards  
Sandy

---

<div class="post-metadata">

**Author:** ![rlgandu](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/rlgandu/32/488436_2.png) [@rlgandu](https://forum.uipath.com/u/rlgandu)\
**Post date:** [July 31, 2024, 2:47pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/10 "2024-07-31T14:47:20Z")

</div>

@rob_lavelle

```auto
If(condition1, If(condition2, valueIfBothTrue, valueIfCondition1TrueAndCondition2False), valueIfCondition1False)

```

This is the syntax

---

<div class="post-metadata">

**Author:** ![ppr](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ppr/32/464097_2.png) [@ppr](https://forum.uipath.com/u/ppr)\
**Post date:** [July 31, 2024, 2:56pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/11 "2024-07-31T14:56:21Z")

</div>

@rob_lavelle  
at a particular level of nested if structures, we recommend focusing on alternates, due an increased readability and other factors.

But this depends on the details of the use case. Feel free to share more details along with sample data with us

---

<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:** [August 3, 2024, 2:57pm UTC](https://forum.uipath.com/t/optimize-the-code-using-assign/757320/12 "2024-08-03T14:57:02Z")

</div>

Closed on August 3, 2024, 2:57pm UTC

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