Page 1 of 1

When does an IF statement need an ENDIF

Posted: Sun Jan 27, 2019 7:51 pm
by PaulKnight
Hi,

Could someone please advise why some IF statements seem to need END IF and sometimes I get an error saying "END IF without IF"?

Any advice much appreciated.

Thanks,
Paul

PS: apologies for what must seem like very obvious questions to most!!

Re: When does an IF statement need an ENDIF

Posted: Sun Jan 27, 2019 8:59 pm
by rbytes
All IF...THEN statements must have a matching ENDIF or END IF. If you receive a warning that you are missing one of the two, then you have an error in your coding. Don't forget THEN at the end of the IF statement.

It isn't always obvious where the error is occurring. That's why it is a good idea to indent the lines following an IF..THEN, and outdent the ENDIF.

You can include an ELSE statement between IF..THEN and ENDIF, but the above rule still applies.

Re: When does an IF statement need an ENDIF

Posted: Mon Jan 28, 2019 9:54 am
by Dutchman
An IF/ELSE statement on a single line needs no ENDIF.
See examples in PDF-manual viewtopic.php?f=79&p=13482#p13482 on page 15:

IF / ELSE
Single-line:
IF A = 0 THEN GOTO 10 ELSE GOTO 20
Short version:
IF A = 0 THEN 10 ELSE 20

IF / ELSE / END IF
Multiple-line:

Code: Select all

IF X < 0 OR Y > 0 THEN 
   A= 0
ELSE
  A= 1
END IF

Re: When does an IF statement need an ENDIF

Posted: Mon Jan 28, 2019 11:59 am
by Henko
One further remark:

If <cond> THEN <any statement> does not need an END IF

but when more statements are on one line (separated by the "!" mark),

If <cond> THEN ! <statement> ! <statement> ...... ! END IF, the END IF is nessecary.

For clarity, it is better to put each statement on a separate line (although for myself i use to write compact code).