# Using conditional statements in Field Calculator

I have two fields, "P_Area" and "PC_Area", whose sizes I'd like to compare in a third field, "Areas_Equal". The "Areas_Equal" field should contain a "Yes" if the other two are equal and a "No" if they aren't.

Could someone please tell me how I can do this with Field Calculator? In Python or VB. I've tried a both based on snippets I've found online, but I'm doing something wrong because I keep getting an error message.

-

## 2 Answers

If you're using Python in ArcGIS you may be using an `=` for comparison instead of an `==`

Your code should be similar to the below, if you use Python:

Pre-Logic Script Code - Check "Show Codeblock"

``````def ifBlock(pArea,pcArea):
if pArea == pcArea:
return 1
else:
return 0
``````

Then in the actual code:

``````ifBlock(!P_AreaFieldName!,!PC_AreaFieldName!)
``````
-
And, of course, if he wants "Yes" and "No" instead of 1 and 0, he should use `return "Yes"` and `return "No"`. – nmpeterson Mar 19 '12 at 13:44
Thanks very much for that, worked a charm. – jay Mar 19 '12 at 20:59
Another way to write this in Python (that doesn't require the code block) is this... `1 if !P_Area! == !PC_Area! else 0` – Jason Miller Aug 12 '14 at 2:35

Please always mention which tool you are using. I will explain how to do this in QGIS because it's free.

Open your shapefile. Open the attributes table. At the bottom create a new columon, call it 'test' or whatever. Now click on advanced search. Type 'P_Area = PC_Area' (without quotes) in SQL where clause. Hit OK.

Now click on 'Open field calculator' (ctrl+I). On the top check 'update existing field' and 'only update selected features'. In field calculator, type '1'. Hit ok.

Now, go back to advanced search, this time search for 'P_Area != PC_Area'. Do same steps as above, except input '0' in field calculator instead of '1'.

I am sure you can do it in a smarter way, but hey, this works. :)

-