if elseif else 在 Netlogo 环境中的实现

if elseif else implementation in Netlogo environment

我希望 - if elseif else 在 NetLogo 中声明。我怎样才能有效地做到这一点?我检查了 NetLogo 文档,没有命令这样做。 Previous similar question没有直接回答而是结合上下文解决了

一个简单的解决方案是:

    let flag true
    if(condition1)
    [
    ...
    set flag false
    ]
    if(flag and condition2)   ;else if statement
    [
    ...
    set flag false
    ]
    if(flag)  ;else statement
    [

    ...
    ]

我正在寻找其他更高效的。

编辑: 根据 Nicolas 的建议,在第二个 if 条件中添加了标志。

唯一能真心推荐的方法是:

ifelse condition1
  [ ... ]
  [ ifelse condition2
      [ ... ]
      [ ifelse condition3
        [ ... ]
        [ ifelse ...

但是,是的,缩进和可读性不是很好。有关可能的最终改进的想法,请参阅 https://github.com/NetLogo/NetLogo/issues/344 and https://github.com/qiemem/ControlFlowExtension

我发现这种形式最易读,尽管末尾的一堆“]”有点令人不快

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][
]]]]]]

优点是简洁,读起来像"C"风格switch语句