if-else

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...

Emulating SWITCH and SELECT CASE

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.

Example 1 -- No default action
Example 2 -- With Default Action
 
guide/ifelse.txt · Last modified: 2009/07/17 14:31 by 67.149.64.123
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki