ELSEIF

<< Click to Display Table of Contents >>

Navigation:  Gekko User Manual > Gekko statements >

ELSEIF

Previous pageReturn to chapter overviewNext page

An ELSEIF statement can be used to filter through different conditions and is used in conjunction with an IF condition and an END statement. The syntax for this is simpler than using ELSE and IF instead, cf. the examples.

 


 

Examples

 

ELSEIF can be used to sift through possibilities, like this:

 

%= 1;
if (%== 1);
  //do something when 1
elseif (%== 2);
  //do something when 2
elseif (%== 3);
  //do something when 3
else;
  //do something when not 1, 2 or 3
end;

 

The same can be done without using ELSEIF, but in that case a number of END's need to be added:

 

%= 1;
if (%== 1);
  //do something when 1
else; if (%== 2);
  //do something when 2
else; if (%== 3);
  //do something when 3
else;
  //do something when not 1, 2 or 3
end; end; end;

 

These END's are inconvenient and stems from the fact that the above code can be reorganized into the following structure:

 

%= 1;
if (%== 1);
  //do something when 1
else; 

  if (%== 2);
    //do something when 2
  else; 

    if (%== 3);
      //do something when 3
    else;
      //do something when not 1, 2 or 3
    end; 

  end; 

end;

 

Here it is more clear why there are three ending END's. Using ELSEIF avoids that.

 


 

Related statements

 

IF, ELSE, END, FOR