# Calculator with if/else statement

**URL:** https://discourse.paraview.org/t/calculator-with-if-else-statement/13667
**Category:** ParaView Support
**Created:** [January 10, 2024, 2:29pm UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667 "2024-01-10T14:29:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![SCRO1022](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@SCRO1022](https://discourse.paraview.org/u/SCRO1022)
#### Post date: [January 10, 2024, 2:29pm UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667/1 "2024-01-10T14:29:45Z")

</div>

My problem right now is, that i would like to have a calculator function which first checks if a specific scalar (CONC\_001) is greater than 0.1. If that is the case it should simply calculate: CONC\_003 = CONC\_001 / CONC\_002. Is CONC\_001 smaller than 0.1 CONC\_003 should be set to 1000.

Is there a filter to do this check in order to create new cell data with either 0 or 1. So then i could use the simple calculator filter for the final calculations.

Thanks a lot 🙂

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [January 10, 2024, 3:15pm UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667/2 "2024-01-10T15:15:29Z")

</div>

you should be able to do that with the `Python Calculator`, and a one liner like `result if cond else othervalue`

---

<div class="post-metadata">

### Author: ![SCRO1022](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@SCRO1022](https://discourse.paraview.org/u/SCRO1022)
#### Post date: [January 10, 2024, 3:45pm UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667/3 "2024-01-10T15:45:42Z")

</div>

Thank you, i already tried it like this:  
value\_when\_true if condition else value\_when\_false  
But somehow it only takes the values of “value\_when\_true”. It doesn’t check the if condition.

Is it important that it is a Multi-Block DataSet? Because when i do “MergeBlocks” and then use the PythonCalculator with “np.where(condition, value\_when\_true, value\_when\_false)” it works fine. But i want to avoid merging the blocks.

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dave_demarle/32/27_2.png) [@Dave\_DeMarle](https://discourse.paraview.org/u/Dave_DeMarle)
#### Post date: [January 10, 2024, 4:20pm UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667/4 "2024-01-10T16:20:05Z")

</div>

I agree with Nicolas that Python Calculators should be preferred, but I will note that in a pinch the old style calculator does support ternary style if expressions.  
Test with Wavelet and this Calculator expression:  
“if(coordsX\>1,1,0)”

---

<div class="post-metadata">

### Author: ![SCRO1022](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@SCRO1022](https://discourse.paraview.org/u/SCRO1022)
#### Post date: [January 10, 2024, 4:52pm UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667/5 "2024-01-10T16:52:48Z")

</div>

Wow i didn’t know the normal calculator is able doing this. This works fine for me 😃 thank you very much. But can you explain why the same code doesn’t work in the PythonCalculator?

---

<div class="post-metadata">

### Author: ![spyridon97](https://discourse.paraview.org/user_avatar/discourse.paraview.org/spyridon97/32/11569_2.png) [@spyridon97](https://discourse.paraview.org/u/spyridon97)
#### Post date: [January 11, 2024, 9:08am UTC](https://discourse.paraview.org/t/calculator-with-if-else-statement/13667/6 "2024-01-11T09:08:03Z")

</div>

The original calculator works with if statements because now it has the exprtk library as backend. I can’t answer about the Python calculator.
