Reputation: 9796
I wish - if elseif else
statement in NetLogo. How can I do so efficiently? I checked NetLogo documentation no command to do so. Previous similar question didn't answer it directly but solved in the context.
One simple solution is:
let flag true
if(condition1)
[
...
set flag false
]
if(flag and condition2) ;else if statement
[
...
set flag false
]
if(flag) ;else statement
[
...
]
I am looking for others more efficient ones.
Edit: added flag in the second if condition upon Nicolas's suggestion.
Upvotes: 5
Views: 1007
Reputation: 2096
I find this form the most readable although the stack of "]" at the end is a little off-putting
ifelse item cur brain = 0 [sit][
ifelse item cur brain = 1 [eat][
ifelse item cur brain = 2 [steal][
ifelse item cur brain = 3 [birth][
ifelse item cur brain = 4 [hunger][
ifelse item cur brain = 5 [smell][
]]]]]]
it has the advantage of being concise and reading like a "C" style switch statement
Upvotes: -1
Reputation: 30453
The only way I can wholeheartedly recommend is:
ifelse condition1
[ ... ]
[ ifelse condition2
[ ... ]
[ ifelse condition3
[ ... ]
[ ifelse ...
But yeah, the indentation and readability aren't great. For thoughts on possible eventual improvements, see https://github.com/NetLogo/NetLogo/issues/344 and https://github.com/qiemem/ControlFlowExtension.
Upvotes: 4