# Conditional evaluation with Con

The Con tool allows you to control the output value for each cell based on whether the cell value is evaluated as true or false in a specified conditional statement.

If the cell is evaluated as true, it will receive one value; if it is evaluated as false, it will receive another. The values a cell should receive when it is evaluated as true are specified by the Input true raster or constant value. The values a cell should receive when it is evaluated as false are specified by an Input false raster or constant.

Conceptually, during execution, the Con tool visits each cell location and, based on the cell's value and the conditional statement, determines if the cell is evaluated as true or false. If the cell is evaluated as true, the output value for that location is identified in the true input. If the cell is evaluated as false, the output value for that location is identified in the false input.

In the geoprocessing environment, there are two ways to identify whether a cell location is evaluated as true or false: by an input raster or by an input raster in which an optional input expression is applied. If only an input raster is used, all nonzero values in the input raster are considered true and all zero values false. Cells assigned NoData will receive NoData as output. NoData does not equate to false.

A number of Spatial Analyst tools can be used to create a conditional input raster. In particular, the logical tools in the Math toolbox can be used, with the Test tool being particularly useful.

## Using the Con tool in Map Algebra

To perform conditional evaluation on a raster dataset in Map Algebra, input the raster dataset as conditional raster to the Con tool. Input a true raster to provide values to be returned when the conditional evaluation is true. Input a false raster to provide values to be returned when the conditional evaluation is false. The where clause parameter defines the expression to be evaluated against the input conditional raster.

### Example

For example, if you want to assign the value of 10 (identified as the true constant) as good for construction and 1 (identified as the false constant) as unsuitable for construction based on the slope at a cell (identified by the input conditional raster), and less than 15 percent slope is considered good, you would enter the expression "value < 15". If a cell has less than 15 percent slope, it will receive the value of true (in this case, 10); otherwise, it will receive the value identified by false (in this case, 1).

``OutRas = Con(SlopeRas, 10, 1, "VALUE < 15")``

## Using complex expressions with the Con tool in Map Algebra

In Map Algebra, more capabilities can be attained with the expression than a simple logical condition. With a complex expression, you can, for example, have several individual expressions be nested in it, specify multiple rasters, or use other tools and operators.

Any valid Map Algebra expression that results in a raster can be used as an argument for any of the input conditional, true, or false raster inputs.

Note that the <where_clause> parameter should be dropped if the input conditional raster expression is used in a complex expression. In this case, the syntax of the Con tool takes on the following generalized form:

` Con(in_conditional_raster, true_raster, {false_raster})`
The <where_clause> parameter is dropped in this case because the condition raster is provided by the map algebra expression, which would return a raster dataset, for example, with values of 0 and 1 if it were a logical operation.

### Examples

• An example of using a complex expression in the Con tool is:
``OutRas = Con(InRas < 15, 10, 1)``
In the above expression, if the value of a cell in InRas is less than 15, 10 will be assigned to that cell location (true) on the output raster; otherwise, cell values greater than or equal to 15 will be assigned 1 (false) on the output raster.
• If no value or expression is specified for the false expressions:
``OutRas = Con(InRas < 15, 10)``
The results will be the same as the above output except the cells that have a value of 15 or greater will be assigned NoData.
• Any valid expression can be used in place of a value for the <true_expression> and <false_expression> arguments.
``OutRas = Con(InRas1 > 5, Sin(InRas1), Cos(InRas1))``
In the above expression, the sine of all values greater than 5 and the cosine for all values of 5 or less are calculated, and the results are sent to OutRas.
• Multiple conditional statements can be used within the Con tool, but each must have a value or expression <true_expression> that can be used to assign values to the output cells if the result of the evaluation of the condition is true. The optional value or expression {false_expression} can be applied if none of the results of the evaluations of the conditions are true.
``OutRas = Con(InRas1 < 5, Sin(InRas1), Con(InRas1 < 20, Cos(InRas1), Con(InRas1 > 50, 100, 0)))``
In the above expression, the sine is calculated for those values that are less than 5, the cosine is calculated for the values that are 5 or greater but less than 20, the values that are 20 or greater but 50 or less are assigned to 0, and the values greater than 50 are assigned 100.
• Multiple conditions can be used in a conditional expression of the Con tool.
``OutRas = Con((InRas1 > 5) & (InRas1 < 10), 5, 100)``
In the above expression, if the input value from InRas1 is greater than 5 and less than 10, assign the location to 5; otherwise, assign it to 100.
• Tools and operators can be applied to the input rasters in the conditional expression and the results evaluated.
``````OutRas = Con(Sin(InRas1) > .5, 10, 100)
OutRas2 = Con((InRas1 + InRas2) > 10, 100, 5)
OutRas3 = Con(InRas1 > 5, Cos(InRas1), Sin(InRas1))``````
• A Con tool can be nested within another Con tool.
``OutRas = Con(InRas1 > 23, 5, Con(InRas1 > 20, 12, Con((InRas1 > 2) & (InRas1 < 17), Sin(InRas1), 100)))``
• Multiple rasters can be used in the conditional statement or in the expression to be performed on the cells.
``````OutRas = Con(InRas1 + InRas2 > 7, Sin(InRas1), Cos(InRas2))
OutRas2 = Con(InRas1 < 9, InRas1 * InRas2 + Tan(InRas3), Cos(InRas1))``````
The input of multiple rasters is possible since the Con tool is evaluated for each x,y cell location before moving to the next cell. When multiple rasters are input, the specified operator or tool will process on a cell-by-cell basis between the multiple rasters.