0

In the MiniZinc tutorial, I noticed that the endif keyword was repeated many times at the end of a series of conditional statements. Is it possible to write switch statements in MiniZinc as an alternative to this verbose syntax?

For example, I'd like to write this series of conditional statements more concisely:

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;
Anderson Green
  • 30,230
  • 67
  • 195
  • 328

1 Answers1

3

The multiple "endif" are not necessary. You can use "elseif" instead of "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;

Note: If you want a (simple) lookup table, you might use the global constraint "table" instead. See section 4.1.3 in MiniZinc Tutorial for an example.

hakank
  • 6,629
  • 1
  • 17
  • 27
  • 1
    Surprisingly, [the MiniZinc tutorial](http://www.minizinc.org/downloads/doc-latest/minizinc-tute.pdf) does not mention the `elseif` syntax that you described here. I hope it will be updated soon. – Anderson Green May 28 '17 at 22:07
  • Yes, "elseif" is not mentioned, but there's a lot of example models with `else if`. Perhaps "elseif" is deprecated now. – hakank May 29 '17 at 15:55
  • It's more likely that the tutorial is out of date. The `elseif` syntax is much more concise and readable than the one in the tutorial. – Anderson Green Jul 10 '17 at 17:14