0

MiniZinc 教程中,我注意到endif关键字在一系列条件语句的末尾重复了很多次。是否可以在 MiniZinc 中编写 switch 语句来替代这种冗长的语法?

例如,我想把这一系列条件语句写得更简洁:

predicate examplePredicate(var int:x, int:s) =
if s == 1
    % some code goes here
else if s == 2 then
    % some code goes here
else if s == 3 then
    % some code goes here
else if s == 4 then
    % some code goes here
else
    % some code goes here
endif endif endif endif;
4

1 回答 1

3

多个“endif”不是必需的。您可以使用“elseif”代替“else if”。

predicate examplePredicate(var int:x, int:s) =
  if s == 1
     % some code goes here
elseif s == 2 then
     % some code goes here
elseif s == 3 then
     % some code goes here
elseif s == 4 then
     % some code goes here
else
     % some code goes here
endif;

注意:如果你想要一个(简单的)查找表,你可以使用全局约束“table”来代替。有关示例,请参见 MiniZinc 教程中的第 4.1.3 节。

于 2015-09-26T05:06:37.063 回答