ifthen4.gms : Tests $ifthen from old release notes

Description

A new variant on the $if statement has been introduced. It follows the
usual structures and allows appropriate nesting. The syntax for the
condition are the same as for the $if statement. The $ifthen and
case insensitive compare and E for constant expression evaluation. In
the example below we will execute all blocks of such a statement.

Contributor: Alex


Small Model of Type : GAMS


Category : GAMS Test library


Main file : ifthen4.gms

$title Tests $ifthen from old release notes  (IFTHEN4,SEQ=454)

$ontext
A new variant on the $if statement has been introduced. It follows the
usual structures and allows appropriate nesting. The syntax for the
condition are the same as for the $if statement. The $ifthen and
$elseif have the same modifiers as the $if statement, namely I for
case insensitive compare and E for constant expression evaluation. In
the example below we will execute all blocks of such a statement.

Contributor: Alex
$offtext


$ondollar
$maxgoto 10 $set x a
$label two
$ifthen %x% == a $set x 'c' $log $ifthen   with x=%x%
$elseif %x% == b $set x 'k' $log $elseif 1 with x=%x%
$elseif %x% == c $set x 'b' $log $elseif 2 with x=%x%
$else            $set x 'e' $log $else     with x=%x%
$endif $if NOT %x% == e $goto two

$eval x 1
$label three
display 'x=%x%';
$ifthen %x% == 1 $eval x %x%+1
$elseif %x% == 2 $eval x %x%+1
$elseif %x% == 3 $eval x %x%+1
$elseif %x% == 4 $eval x %x%+1
$else            $set  x done
$endif $if NOT %x% == done $goto three


*This is a bit contrived but illustrates some of more subtle features. Anytime we use a looping construct via a $goto statement we have to protect ourselves against the potential of an infinite loop. The number of times we jump back to a label is counted and when a limit is reached, GAMS will issue an error. It is important to note that the %string% references are substituted only once.

*Lengthy and nested ithen/else structures can become difficult to debug. Tagging of the begin, the $ifthen and the end, the $endif can be helpful. For example, the next line will fail because the tags do not match:

$ifthen.one x == x
$endif.one

*As with the $if statement, the statement on the line with the $ifthen style statements is optional. The following two statements give the same results:

$echo $remark this is abc > abc.gms
$echo $remark this is efg > efg.gms
$echo $remark this is xyz > xyz.gms

$set type 'low'
$label lower
$iftheni %type% == low $include abc
$elseifi %type% == med $include efg
$else                  $include xyz
display 'xyz';
$endif
display 'first part lower';

$iftheni %type% == low
$include abc
$elseifi %type% == med
$include efg
$else
$include xyz
display 'xyz';
$endif
display 'second part lower';

$ifi %type%==low $set type 'med' $goto lower
$ifi %type%==med $set type 'xxx' $goto lower

* The statements following directly a $ifthen, $elseif, or $else on the same line can be a sequence of other dollar control statements or contain proper GAMS syntax. The statements following directly a $endif can only contain another dollar control statements.

$ifthen.two   c==c  display 'true for tag two';
$ifthen.three a==a  $log true for tag three
display '   then clause for tag three';
$ifthen.four x==x display 'true for tag four';
$log true for tag four
$else.four
display '      else clause for tag four';
$endif.four          $log endif four
$endif.three         $log endif three
$endif.two           $log endif two

$exit

This will produce a GAMS program like

1  display 'true for tag two';
3  display '   then clause for tag three';
4  display 'true for tag four';

with the following log output

--- Starting compilation
true for tag three
true for tag four
endif four
endif three
endif two