|
<< Click to Display Table of Contents >> IF |
![]() ![]()
|
The IF statement is used for conditional execution of blocks of statements, in the form if (...); ... ; else; ... ; end;. The expression inside the IF parenthesis is typically of the form ... [op] ..., where ... are two expression, and [op] is an operator like ==, <>, etc. If both expressions are scalar expressions, the IF just compares the two parts given an operator <, <=, ==, >=, > or <> , possibly combined with and, or or not. When using and or or, several comparisons can be made at the same time, like for instance if (%t >= 2020q1 and %t <= 2029q4), where there are two ... [op] ... comparisons.
Guide: conditional statements For an easier introductory guide on conditional statements in Gekko, see this page. |
The expressions do not need to evaluate to scalar values: you may also use IF with timeseries expressions, for instance if (x == 1), which tests if all values of x are == 1 (given the global time period). With two timeseries like if (x == y), the IF tests if all values of x and y are equal (again given the global time period). IF-statements also work without operator, for instance if (%x); ...; end; or if (x); ... ; end;. In that case, the IF statement is true if the value %x does not have the value 0, or if the series x does not contain any 0 value over the global time period. For timeseries comparisons like if (x > y); ...; end;, the IF will be true if all of the observations of x are larger than y (given the global time period). Note: If you are comparing expressions that contain missing values, special rules apply: see this section.
If you need to perform IF-like operations inside a SERIES (that is, one IF-like operation for each period in the global time period), you may use $-conditionals on expressions, or the iif() function. See examples in on SERIES help page.
You may sometimes need to explicitly convert the variables in order to compare them (for instance by means of the functions val(), date() or string()).
if (expression1);
statements1... ;
elseif (expression2);
statements2... ;
else;
statements3... ;
end ;
expression |
A logical expression involving strings, dates or values (or single timeseries observations like x[2010]), in addition to the logical operators and, or, not, <, <=, ==, >=, >, <>. Please note that logical equivalence uses == (and not = which is assignment) , and that you must use <> for logical difference, not for instance !=.
If the expression is a scalar value, the statements are not executed if the scalar value is 0, and are executed otherwise. |
statements1 |
Gekko statements to be executed if expression1 is true. |
statements2 |
Gekko statements to be executed if expression1 is false and expression2 is true. (You may use as many ELSEIF's as you please). |
statements2 |
(Optional). Gekko statements to be executed if expression1 and expression2 are both false. |
See more examples involving IF in loops in the FOR help file. A very simple example using the string scalar variable.
%write = 'yes'; |
To sift through more choices, you may use IF, ELSEIF, ELSE and END like this:
%windows = '11'; |
Inside the IF or ELSEIF parentheses you may use logical conditions like and or or:
%v = 2000; |
This will print 'Ok'. Note that you have to explicitly convert the variables to be able to compare them, otherwise you will get an error. In this case, the conversion functions val() and date() are used. The conversions may seem obvious and superfluous here, but consider this example:
%s = '100'; |
This gives an error, because Gekko does not know how to compare the value 133 with the string '10033' (the sum of the two strings taken literally). So
if (%v == val(%s + %s1)) //will be false, comparing 133 and 10033 |
or this:
if (%v == val(%s) + val(%s1)) //will be true, comparing 133 and 133 |
To avoid ambiguities, the type system in Gekko is quite strict. To access individual observations from a series in the first-position databank, use the variable[period] syntax:
time 2010 2012; |
This will print 'Ok'. To compare and transform timeseries depending upon individual observations, see the iif() function.
The $-conditional can often be used instead of IF, for instance:
time 2014 2016; |
The IF can be stated in the following simpler way, using the $-conditional. You may also use the similar iif() function.
y = 2 $ (x[2015] == 100); |
If the $-conditional is false, it will use 0 as value. To compare two series, or a series and a value, use the following:
time 2001 2003; //Result: // larger // larger than 1 // --------------------------- //Result: // not larger // not larger than 1 |
As seen, for the ... > ... condition to be true in the above example, it needs to be true for each period in the global time period (2001-2003).
•Note the use of two equal signs (==) in IF-statements, and <> for logical difference.
•There is also an exist(x) function that returns 0 or 1 depending upon whether the series x exists or not.
•See the GAMS-inspired $-condition or the iif() function for logical conditions inside timeseries observations.
•You may ask a list if it has a particular member (will return 1 for true, and 0 for false). For instance 'a' in #m is true if #m contains 'a'. Therefore, you may for instance use if('a' in #m) without logical operator. The alternative syntax if(#m.contains('a')) is equivalent.
iif()