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