# Nested Conditional Staement with QGIS Raster Calculator

I like to evaluate the following expression in the QGIS Raster Calculator: IF

``````ELSEIF(A = 1, 1, ELSEIF((B = 1 OR B = 2), 3, ELSEIF((C = 1 OR C = 2), 4, ELSEIF(D = 1, 5, DEM))))
``````

I tried the following expression in the Raster Calculator Console:

``````("A"=1)*1 + ("B" = 1)*3 +("B" = 2)*3 + ("C" = 1)*4 + ("C" = 2)*4 + ("D" = 1)*5 + "DEM"
``````

The idea is to get values from 1 - 5 if some of the first conditional statements are fullfilled when evaluating for the rasters that are not the Digital Elevation Model, if any of those conditions are not fullfilled then it should get the value of the Digital Elevation Model. However when I run the calculation I get weird results that seems to be the sum of values in the Digital Elevation Model and some of the rasters.

What is the logic when using the raster calculator in QGIS to perform nested conditional map algebra?

• Perhaps explore using "CASE WHEN a =1 THEN b, WHEN c = 2 THEN d" docs.qgis.org/3.16/en/docs/user_manual/working_with_vector/… Commented Dec 8, 2020 at 8:33
• When one condition is met, such condition has to negate all following terms at the same time. You can do it by adding `("A" != 1)*(following terms)` and so on, but it will be an awfully long syntax. Why don't you use ifelse() function from SAGA Raster Calculator (in the Processing Toolbox)? Commented Dec 8, 2020 at 8:55
• Thank you very much Kazuhito. The problem with the SAGA Raster Calculator is my lack of understanding about how to name the rasters. In the Standard Raster Calculator from QGIS I get a panel where I can only click on the raster's name and is automatically added to the Expression, but in SAGA I don't know if I have to provide the layers name, or not. I tried so, and I received some Errors. Commented Dec 8, 2020 at 9:13
• SAGA in QGIS will call layers as `a, b, c, d...` or `g1, g2, g3, g4...` according to the order they were supplied in the layer selection window. In this case, something like `ifelse(a=1, 1, ifelse(or(b=1, b=2) ,3, .........))`. Commented Dec 8, 2020 at 9:49
• Great advice! I will try it out. Thank you @Kazuhito Commented Dec 8, 2020 at 9:51