Blog
From Code to Make
if - else if - else Logic
01 Mar 2024
An if - else if - else construction in programming languages allows testing conditions and, depending on the result, executing different blocks of code. This is essential for creating dynamic outcomes. Below, I'll show how you can apply this principle in Make as well.
The If-Statement
The if-statement is the starting point. It determines whether a specific condition is true. If so, the code within the if-statement is executed. If not, the program skips this code.
Example in Javascript:
Make
In Make, the same logic is applied through the use of filters. It's possible to define a filter between two modules to apply the same logic as the Javascript example.
Only if the filter condition is true, Make will continue the process after this filter.
The Else If Statement
The else if statement follows the if-statement and is used to test additional conditions if the original if-condition is not true. Multiple else-if statements can be used in succession to check for various scenarios.
Example in Javascript:
Make
This can be achieved in Make by expanding the initial example with a router. This router allows adding multiple paths that are executed sequentially.
A new filter can be defined on each path. To mimic the Javascript example, two paths are needed. Since Make executes paths sequentially, we need to adjust the condition in Make so it can only be true if the temperature is indeed lower or equal to 25 degrees and greater than 15 degrees. This highlights a significant difference from traditional programming: in Make, other filters will still be evaluated even if a filter on a previous branch has been evaluated as true. This emphasizes the importance of carefully defining your filters in Make to ensure they deliver the desired results, regardless of the sequential evaluation of the paths.
The Else Statement
Finally, the else statement catches all cases not addressed by the preceding if or else if statements. It acts as a catch-all for any remaining possibilities.
Example in Javascript:
Make
In Make, you can define one of the paths within a router as a fallback route. This path is executed only if none of the defined filters are found to be true.
Note that a fallback route is indicated in the router module by a different icon.
Pro Tip
If you want to adjust the order of your paths or view the current order, you can open a menu by right-clicking on a router and selecting 'Order routes'. This menu provides an overview of the current order and allows you to adjust it.
Pro Tip 2
Between two modules, you can not only place a filter on any path but also add a label. This gives you the opportunity to visually represent what the filter is about, making your workflows clearer and easier to understand.