Page 1 of 1

Elseif Question

Posted: January 20th, 2012, 5:19 pm
by Guest
This may be a dumb question, but is there a difference in using “else if” and using “elseif” in CSPro logic?

Can it be written as two words or is it always written as one word?

Thanks.

Re: Elseif Question

Posted: April 5th, 2012, 2:44 am
by Gregory Martin
There is a slight difference between the two formulations. The two following blocks of code do the same thing:
if A then
elseif B then
else
endif;

if A then
else if B then
else
endif;
endif;
Why is a second endif needed in the second block? It helps to indent the code to understand why:
if A then
else
    
if B then
    
else
    
endif;
endif;
Each else needs to be matched with an endif, whereas a grouping of if/elseif/else only must be matched with one endif. So effectively you can use "if" or "else if" to accomplish the same conditional checks, but with the latter syntax you will have many more endif statements, which might make your code look more awkward.