Blog

From Code to Make

if - else if - else Logic

01 Mar 2024

Make scenario: if - else if - else explanation
Make scenario: if - else if - else explanation

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.

Showcase router logic

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:

if (temperature > 25) { 
  console.log("It's warm outside!"); 
}

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.

Make scenario: filterMake scenario: if filter - definition

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:

if (temperature > 25) {
  console.log("It's warm outside!");
} else if (temperature > 15) {
  console.log("It's pleasant outside!");
}

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.

Make scenario: router

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.

Make scenario: Router met 2 aftakkingen

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:

if (temperature > 25) {
  console.log("It's warm outside!");
} else if (temperature > 15) {
  console.log("It's pleasant outside!");
} else {
  console.log("It's cold outside!");
}

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.

Make scenario: Router met fallback route

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.

Make scenario: Sorteer aftakkingen van een router

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.

Make scenario: Paden met labels