|
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.
If (logical condition, statements to be executed If true ;);
If (logical condition, statements executed If condition true; else statements execut. If cond. not true;);
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 );
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: 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:
|