When does an IF statement need an ENDIF

Post Reply
PaulKnight
Posts: 19
Joined: Mon Jan 21, 2019 9:27 pm
My devices: iPhone SE
Flag: Great Britain

When does an IF statement need an ENDIF

Post 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!!

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: When does an IF statement need an ENDIF

Post 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.
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 848
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: When does an IF statement need an ENDIF

Post 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
Last edited by Dutchman on Fri Jan 20, 2023 12:01 pm, edited 1 time in total.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: When does an IF statement need an ENDIF

Post 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).

Post Reply