2015-09-26 142 views
0

MiniZinc tutorial,我注意到endif關鍵字在一系列條件語句的結束重複多次。是否可以在MiniZinc中編寫switch語句來替代這種冗長的語法?switch語句中MiniZinc

例如,我想更簡明地寫這個系列的條件語句:

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; 

回答

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; 

注意:如果你想有一個(簡單)的查找表,您可以使用全局約束「表」來代替。例如,請參閱MiniZinc教程中的第4.1.3節。

+1

出人意料的是,[在MiniZinc教程](http://www.minizinc.org/downloads/doc-latest/minizinc-tute.pdf)沒有提到你在這裏所描述的'elseif'語法。我希望它會很快更新。 –

+0

是的,「elseif的」沒有被提及,但有很多例子車型與其他'if'。也許「elseif」現在已被棄用。 – hakank

+0

它更可能是本教程是過時的。在'elseif'語法比一個教程更簡明易讀。 –