$ifthen, iftheni, ifthene, else, elseif, endif  conditionals

Top  Previous  Next

$Ifthen and the other components below are a form of a $IF that controls whether a number of statements are active. An $IFTHEN must be matched with a $ENDIF.  The syntax for the condition are generally the same as for the $if statement. The $ifthen and $elseif have variants that are case insensitive ($IFi and $ELSEIFi) or evaluate numerical values of the control variables  ($IFe and $ELSEIFe).

Notes:

$IFTHENe is used to do numerical comparisons
$IFTHENi is used to do case insensitive comparisons, while $IFTHEN does case sensitive ones
$ELSEIF has another comparison behind it as in the example below
$ELSEIFi is a case insensitive variant of $Eleseif
$ELSEIFe is a numerical value evaluating variant of $Eleseif
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.
A NOT maybe used in the commands
The comparisons allowed are covered in the forms of conditionals section
One may add a tag to the IFTHEN and ENDIF conditions to force them the match up such as in (setcontrol.gms)

 $ifthen.onea x == x

          display "it";

 $ifthen.twoa a == a

             display "it2";

 $endif.twoa

 $endif.onea

  The evaluation of expressions follows the rules given under the discussion of $Eval

 

Examples:

(setcontrol.gms)

$setglobal aroundit

$ifthen setglobal aroundit

   display "statement 1";

   display "statement 2";

$else

   display "statment 3";

$endif

 

Here when aroundit is setglobal then we get the two displays executed.  Otherwise the third one occurs.

 

Much more complex forms can be used

 

$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

 

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:

$iftheni %type% == low $include abc  

$elseifi %type% == med $include efg

$else                  $include xyz

$endif

 

$iftheni %type% == low 

$include abc  

$elseifi %type% == med 

$include efg

$else                  

$include xyz

$endif

 

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

display '      else clause for tag four';

$endif.four          $log endif four

$endif.three         $log endif three

$endif.two           $log endif two

 

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