Tutorials

Conditionals + Switch Statements

When building backend logic, a simple if/else structure can only take you so far. Xano now gives you more expressive tools to route your logic precisely — including expanded conditionals with else-if clauses and a powerful switch statement. Here's how to use both to build smarter, more flexible function stacks.

Expanding Conditionals with Else-If

The classic conditional in Xano worked on a binary basis: if a condition is true, execute one block; otherwise, fall through to else. Now you can chain multiple else-if clauses between the initial if and the final else, giving you as many evaluation steps as you need.

Xano evaluates each clause sequentially. The moment it finds one that evaluates to true, it executes that block and stops. If none of the clauses are true, it falls through to the default else block. You can even nest additional conditionals inside an else clause for more complex branching scenarios.

For example, if you're passing in a color and a format type (like hex or decimal), each combination can have its own clause. Xano works through them in order until it finds a match — and if nothing matches, your else clause handles the fallback gracefully.

Using Switch Statements for Deterministic Logic

Switch statements are a great fit when you know the possible values your input can take and want to handle each one explicitly. Your switch function takes a value and compares it against a series of cases. When a matching case is found, the logic inside that case executes.

One key concept to understand is the break behavior. When "stop after match" is enabled on a case, Xano executes that case's logic and then continues on with the rest of the function stack — meaning you can run additional logic after a match is found, like setting a final response message.

Fall-Through Cases for Grouped Outcomes

Switch statements also support fall-through behavior, which is useful when multiple input values should produce the same result. For instance, if you're mapping days of the week (1 through 7) to "weekday" or "weekend," you can let cases 1, 2, and 3 fall through to a shared break point that sets the value to "it's a weekday." Cases 5, 6, and 7 can similarly fall through to a shared "it's the weekend" result.

This lets you group related outcomes without duplicating logic across multiple cases.

Handling Unmatched Values with Default

Both conditionals and switch statements support a default fallback. If none of your cases or clauses match the input, the default block executes. This is essential for gracefully handling unexpected or unsupported input values — for example, returning a helpful error message when an unrecognized operator is passed into a calculator function.

With these tools at your disposal, you have fine-grained control over how your backend logic branches and responds to data. Experiment with both approaches to find the right fit for each use case in your Xano workspace.

Sign up for Xano

Join 100,000+ people already building with Xano.
Start today and scale to millions.