# Evaluate a variable inside an If clause?

**URL:** <https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299>\
**Category:** Questions\
**Created:** [April 9, 2017, 11:00pm UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299 "2017-04-09T23:00:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alford](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@alford](https://community.wolfram.com/u/alford)\
**Post date:** [April 9, 2017, 11:00pm UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/1 "2017-04-09T23:00:08Z")

</div>

This function

```
fn[x_]=Module[{ww},
 ww=ArcTan[x];
 If[x<ww, ww,(*else*) x]
]

```

evaluates to

```
If[x < ArcTan[x], ww$375, x]

```

Why does the second occurrence of the local variable ww remain as a variable name instead of being evaluated like the first occurrence?

---

<div class="post-metadata">

**Author:** ![neils](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/neils/32/20595_2.png) [@neils](https://community.wolfram.com/u/neils)\
**Post date:** [April 9, 2017, 11:58pm UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/2 "2017-04-09T23:58:12Z")

</div>

You need := in your definition because the = tries to evaluate everything in the assign immediately (which it can’t). You need the delayed assignment.

To be clear: do fn[x\_]:= …

---

<div class="post-metadata">

**Author:** ![bsimpson141421356](https://avatars.discourse-cdn.com/v4/letter/b/ac91a4/32.png) [@bsimpson141421356](https://community.wolfram.com/u/bsimpson141421356)\
**Post date:** [April 10, 2017, 12:15am UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/3 "2017-04-10T00:15:33Z")

</div>

If you look at the help page for If and you click on Details then you find

If[condition,t,f] is left unevaluated if condition evaluates to neither True nor False.

Mathematica recognized your If and then evaluated your condition to try to determine whether it was True or False. To do that it evaluated x and the local variable ww, to do that it determined that ww had been assigned the value ArcTan[x]. You have not assigned a value to x and so the value of x\<ArcTan[x] is just x\<ArcTan[x]. That is neither True nor False and thus it evaluates nothing further and the result is If[x\<ArcTan[x], ww, x]

---

<div class="post-metadata">

**Author:** ![alford](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@alford](https://community.wolfram.com/u/alford)\
**Post date:** [April 10, 2017, 3:08am UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/4 "2017-04-10T03:08:33Z")

</div>

> If[condition,t,f] is left unevaluated if condition evaluates to neither True nor False.

I see, that’s the reason. So I need to _explicitly_ tell mathematica to evaluate it, e.g.

```
fn[x_]=Module[{vv,ww},
 ww=ArcTan[x];
 If[x<ww, Evaluate[ww], Evaluate[(x+ww)/2]
]

```

which yields the desired result

```
If[x < ArcTan[x], ArcTan[x], (x + ArcTan[x])/2]

```

Similarly, with a Which statement I need to sprinkle Evaluate in the right places:

```
fn[x_]=Module[{ww,yy},
 ww=ArcTan[x];
 yy=Exp[x];
 Which[
  x<0, Evaluate[ww],
  Evaluate[x<ww], Evaluate[yy],
  True, Evaluate[2*yy]
 ]
]

```

which yields the desired result

```
Which[x < 0, ArcTan[x], x < ArcTan[x], E^x, True, 2*E^x]

```

Thank you.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [April 10, 2017, 5:57am UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/5 "2017-04-10T05:57:59Z")

</div>

Do I understand it right that you want `fn[z]` to work even if `z` is a symbol (and thus the `If` function will not be evaluated)?

If so, use `With`:

```
Clear[fn]
fn[x_] := With[{ww = ArcTan[x]}, If[x < ww, ww, x]]

fn[z]
(* If[z < ArcTan[z], ArcTan[z], z] *)

```

See here: [https://mathematica.stackexchange.com/questions/559/what-are-the-use-cases-for-different-scoping-constructs](https://mathematica.stackexchange.com/questions/559/what-are-the-use-cases-for-different-scoping-constructs)

One of the uses of `With` is injecting evaluated values.

Or perhaps what you want is `fn` to evaluate only with numbers:

```
Clear[fn]
fn[x_?NumericQ] := With[{ww = ArcTan[x]}, If[x < ww, ww, x]]

```

---

<div class="post-metadata">

**Author:** ![alford](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@alford](https://community.wolfram.com/u/alford)\
**Post date:** [April 18, 2017, 2:54am UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/6 "2017-04-18T02:54:13Z")

</div>

Thank you. You are right about wanting it to work with symbols. Using “With” is a good idea. Also I realized that “Piecewise” is appropriate:

```
fn[x_]=Module[{ww},                                                      
                 ww=ArcTan[x];                                                  
        Piecewise[{{ww,x<ww}},x]                                                
        ] 

```

evaluates to

```
Piecewise[{{ArcTan[x], x < ArcTan[x]}}, x]

```

which does what I wanted.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [April 18, 2017, 10:05am UTC](https://community.wolfram.com/t/evaluate-a-variable-inside-an-if-clause/10299/7 "2017-04-18T10:05:45Z")

</div>

Yes, `Piecewise` is more suitable here because it is explicitly designed for symbolic computations. `If` is primarily for flow control when programming, but it tends to work with many symbolic math functions too.
