"IF ... THEN ... ELSE ..." in plural lines should be followed by "ENDIF" In your case, "ENDIF" is necessary between line 16 and 17.