Click (here) to see the discussion about this primitive.
| Summary | Evaluates the boolean value. If it is true, the first block of commands are executed. Otherwise the second block of commands are executed. |
| Syntax | if-else boolean-value [ commands-if-true ][ commands-if-false ] |
| Version | βnaβ |
| Tags | flow-control |
| Used By | universal |
| CCL Link | dictionary==>if-else |
β Read more...
The switch structure or select/case structure from other languages does not exist in NetLogo. However, something similar can be accomplished in NetLogo using a chain of nested IF-ElSE statements.
The key to using IF-ELSE like this in a maintainable and legible way is to arrange the code to make the programmer's intent clear. Note the use of alignment, bracket-shifting, and short action code in the following example to emphasize the idea that this code selects one from from among several options. Note also how the final line contains all the closing brackets for the structure, and that there is one closing bracket for each option. So, adding another option requires copying one of the existing options, changing is as needed, and adding another close bracket.
Note that if the tests/criteria have overlapping matches, then only the first match action will occur.