If, Else, and Elseif

Top  Previous  Next

Another way of imposing conditionals involves use of the If statement which also involves the else and Elseif statements.  The optional else part allows specification for cases where the If fails.  The Elseif part allows an alternative If test to be presented relative to the original.

The basic syntax for an If statement without an else or Elseif is:

 If (logical condition,

         statements to be executed If true ;);

The basic syntax for an If statement with an else is:

 If (logical condition,

         statements executed If condition true;

 else

         statements execut. If cond. not true;);

The basic syntax for an If statement with an Elseif is:

 If (logical condition,

         statements to be executed If true ;

 Elseif logical condition,

         statements executed If this conditional

         is true and the earlier one is false );

The basic syntax for an If statement with two Elseif s is:

 If (logical condition,

         statements to be executed If true ;

 Elseif logical condition,

         statements executed If conditional is

         true and the earlier one is false ;

 Elseif logical condition,

         statements executed If conditional is

         true and both the earlier ones are false

 );

An else could also be added to 3 or 4 that would be executed when all the previous conditionals were not satisfied.

Examples:

(Ifelseitelse.gms)

A simple If

 

If (key <= 0,

 data1(i) = -1 ;

 key2=case1;

) ;

 

A simple if when $onend is present

 

$onend

If key <= 0 then

 data1(i) = -1 ;

 key2=case1;

endif ;

 

An If with an else

 

If (key <= 0,

 data1(i) = -1 ;

  key2=case1;

else

  data1(i) = data1(i)**3 ;

  key2=case4;

) ;

 

A complex If with more than one Elseif and an else

 

If (key <= 0,

 data1(i) = -1 ;

 key2=case1;

Elseif ((key > -1) and (key < 1)),

 data1(i) = data1(i)**2 ;

 key2=case2;

Elseif ((key >= 1) and (key < 2)),

 data1(i) = data1(i)/2 ;

 key2=case3;

else

 data1(i) = data1(i)**3 ;

 key2=case4;

) ;

 

An If else statement group that contains solve statements.

 

solve ml using lp maximizing z;

If ((ml.modelstat eq 4),

 display 'model ml was infeasible',

          'relaxing bounds on x and solving again';

   x.up(i) = 2*x.up(i) ;

   solve ml using lp minimizing z ;

else

   If ((ml.modelstat ne 1),

 abort "error solving model ml" ;

      );

);

Notes:

When $onend is not present the If is followed by an open ( and a close ) which surround the logical condition and the subsequent statements to be executed.  The logical condition is followed by a comma.
When $onend is present the If is followed by a logical condition a then followed by the subsequent statements to be executed.  The statement is ended with an endif.
The statements executed by an If are ended by the appearance of an Elseif or an else.
One cannot place parameter, acronym, set, file, table, model, equation, variable or scalar statements or .. equation declarations in the statements to be executed when the If logical condition is true.
Many other logical condition forms are possible as explained in the Conditional chapter.
If statements usually can also be written as a set of dollar conditionals, but the If statement may make the code more readable.
The body of the If statement can contain solve statements.