Select to view content in your preferred language

# Nested If or When Statement in Arcade Needs Boolean Test Condition

1800
3
03-04-2022 03:32 PM
New Contributor

Testing this in the Arcade Playground, the error that occurs happens whether I use an IF or a WHEN statement.

The goal is to round up if the number is >= x.75, round down is the number is < x.75 and not to round at all if the number is x.00.

Code:

//Some variables instead of referencing geodatabase fields which Playground doesn't understand.

var Life80=5.5
var LSY=2020

//Determine if date is before or after FY start of September 1
Var RoundDown = Floor(Life80,0);

//Constrain to current year minimum and ridiculously later year maximum

Error:

Execution Error:IF Function must have a boolean test condition

Question: Is there something off about my syntax?  Have been looking through the ESRI community and have seen multiple examples of IF and WHEN statements and the syntax is just as above as far as I can tell.  I feel as if I'm missing something really obvious, but I don't see it.  Any insight would be appreciated!

Tags (5)
1 Solution

Accepted Solutions
by
MVP Esteemed Contributor

The Iif function certainly has its place, but nesting more than one of them makes for hard-to-read code. Consider just breaking it out into separate if/else if/else blocks.

PS - Don't forget about the "insert/edit code sample" button here!

Anyway, testing your same expression in the Playground. The first thing I notice is this:

Life80=RoundDown

In Arcade, this would set the Life80 variable to the value in the RoundDown variable. To check equivalence, you need to use "==". In fact, making that single change gets your expression to evaluate!

But here's that long nested iif, broken up:

``````if (Life80 == RoundDown){
} else if (Life80-0.75 < RoundDown){
} else {
}``````

As you allude to in your post, though, you could certainly do this with When. In your particular situation, it makes sense to use that, as it will be more concise. A full if/else if/else block is useful when the different conditions merely cause your expression to branch off, but since we're just determining the final returned value, it's a bit much. Here's the when version:

``````return When(
Life80 - 0.75 < RoundDown, Constrain(AddRound, 2022, 9999),
)``````
- Josh Carlson
Kendall County GIS
3 Replies
MVP Esteemed Contributor

Try a similar expression.

``````var ar=\$feature.Area_Feet
IIF(ar-Floor(ar,0)>=0.75,Ceil(ar,0),Floor(ar,0))``````

Think Location
by
MVP Esteemed Contributor

The Iif function certainly has its place, but nesting more than one of them makes for hard-to-read code. Consider just breaking it out into separate if/else if/else blocks.

PS - Don't forget about the "insert/edit code sample" button here!

Anyway, testing your same expression in the Playground. The first thing I notice is this:

Life80=RoundDown

In Arcade, this would set the Life80 variable to the value in the RoundDown variable. To check equivalence, you need to use "==". In fact, making that single change gets your expression to evaluate!

But here's that long nested iif, broken up:

``````if (Life80 == RoundDown){
} else if (Life80-0.75 < RoundDown){
} else {
}``````

As you allude to in your post, though, you could certainly do this with When. In your particular situation, it makes sense to use that, as it will be more concise. A full if/else if/else block is useful when the different conditions merely cause your expression to branch off, but since we're just determining the final returned value, it's a bit much. Here's the when version:

``````return When(