在MiniZinc tutorial中,我注意到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;
答案 0 :(得分:3)
多重" endif"没有必要。你可以使用" elseif"而不是"否则如果"。
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节。