Why no endif???

212

Senior Member
I'm playing Picaxe again, and I'm trying to learn these "basic" rules. I'm working on a code for another little video cam project. I'm using a PIR detector and a little board camera connected to an AV transmitter. When motion is detected, I want the camera and transmitter to come on and start sending me videos of the things that go bump in the night :). I have a couple of versions of code that I'm playing with to see what works like I want it to, and what doesn't...and what uses the least power.

My question is:

Why do I not have to put an endif after this if? In fact, if I do put one there, the syntax checker says not to??? Please explain this to me...and please, like you would tell a child, cause I'm still mostly a "basic" illiterate.


standby:

W1 = 0
Do While W1 < 1000 'light checking interval...10,000 is about 50 minutes
Nap 4
W1 = W1 + 1
if pin3 = 1 then video
Loop
goto check_light
 

westaust55

Moderator
IF statements

If you have a single line IF statement such as:

IF something THEN GOTO label ; note that the GOTO is optional​

The end of the instruction line signifies the end of the IF statement.

When you have a multi-line statement such as:
IF something THEN
do something
ELSE
do another thing
ENDIF​
The ENDIF command is required to indicate the end of the IF…THEN structure.
Otherwise the BASIC interpreter will not know where the IF… structure ends.
 

212

Senior Member
Thanks! Yep, I see what you mean with this: (and the second is faster)

main:
IF pin3 = 1 THEN
goto something
endif
goto main

something:
low 1
goto main_2


main_2:
IF pin3 = 1 THEN something_2
goto main_2

something_2:
low 1
goto main_2
 

hippy

Ex-Staff (retired)
(and the second is faster)
In this case it is, but it depends on whether there's an ELSE clause and what happens after the END IF.

Putting a GOTO within an IF-THEN-ENDIF doesn't really make a lot of sense, move the LOW 1 into that block and it would.

If you want absolutely maximum speed and lowest code size then only the IF-THEN-label option should be used and block-structured IF-THEN-ELSE, DO-LOOP, SELECT-CASE and ON-GOSUB commands should not be used. In most cases the small, additional overheads are far out-weighed by the ease of coding, debugging and understandability of the code.
 
Last edited:
Top