If, If not, Ifi, Ifi not, Ife, Ife not

Top  Previous  Next

$If provides control over conditional processing of the input file(s).  The syntax is

 

 $If condition statement to execute

or

 $If NOT condition statement to execute

or

 $Ifi condition statement to execute

or

 $Ifi NOT condition statement to execute

or

 $Ife condition statement to execute

or

 $Ife NOT condition statement to execute

 

The $Ifi variant is case insensitive while $If is case sensitive and the $Ife variant allows constant expression evaluation.

Notes:

Numerous conditional expression types can be used as discussed in the Conditional Compilation chapter.
The result of the conditional test is used to determine whether to include the remainder of the line, which can be any valid GAMS statement including other $ commands like $Goto.
The first non-blank character on the line following the conditional expression is considered to be the 1st column position of the GAMS input line.  Therefore, if the first character encountered is a comment character the rest of the line is treated as a comment line.  Likewise if the first character encountered is the dollar command character, the line is treated as a dollar command line.
An alternative to placing the statement to execute on the same line as the conditional is to leave the remainder of the line blank and place the statement to execute on the line immediately following the $if line.
If the conditional is found to be false, either the remainder of the line (if any) is skipped or the next line is not read.

Example:

scalar a /3/

       b /5/

       c /8/;

 

$if a+b==c display a

$ife a+b==c display b

 

a is not displayed because the $if condition is not true while the $ife condition is true and b is displayed.