If command confusion

alband

Senior Member
I'm going to have to admit to having troble with: if.

I've ckecked the basic commands .pdf and found the if section but there is so much that if can be used for with so many other "else" or "endif" endings an add-ons that I'm lost.
Here's a quote from page 72 in the manual:
Syntax:
IF variable ?? value {AND/OR variable ?? value ...} THEN
{code}
ELSEIF variable ?? value {AND/OR variable ?? value ...} THEN
{code}
ELSE
{code}
ENDIF
This is what I want to say:

Code:
label_3_1:	if pin1 = 1 then let b1 = 1
		goto label_3_2 
label_3_2:	if pin2 = 1 then let b1 = 2
		goto label_3_3
label_3_3:	if pin3 = 1 then let b2 = 1 'yep b2 this time = 1
		goto label_3_4
This doesn't work. Syntax error. How do I express that without needing to create at least six different labels.
It is for a grid referance system that will end up with 7 horizontal and 7 vertical. This "dummy code" just has 2 horiz' and 1 vert'.
Horizontal varible is b1 vertical is b2. when these numbers are displayed with a comma between they give a nice grid referance.
 

Jeremy Leach

Senior Member
If you are doing more than simply goto or gosub, then the code that needs executing if true needs to be a block ending in endif (and it helps to indent it for clarity) ...

Code:
If Pin1=1 Then
    B1 = 1
Endif

If Pin2 = 1 Then
    B1 = 2
Endif

If Pin3 = 1 Then
    B2 = 1
EndIf
 

westaust55

Moderator
Here some info posted about a month ago . . .



When you have a IF . . . . THEN statement the only thing that can (curently) follow on the same line is a re-direction (GOTO or GOSUB) to a label.

Thus IF b3>0 THEN printing
is valid

If you try to put commands after the THEN, the compiler/interpreter does not know when the IF statement has finished.

For example it needs to/could be structured as:


The full structure for the IF...THEN statement is along the lines:

IF something THEN
do one thing
ELSE
do something different
ENDIFThe full structure for the IF...THEN statement is along the lines:

IF something THEN
do_one_thing
ELSE
do something_different
ENDIF


Edit: for completeness . . . .

For a physical one-line statement you can use a new line separate, the colon :))
in which case the format can be:

IF something THEN : do_one_thing : ENDIF
 
Last edited:

alband

Senior Member
Yep, got it working.
Thanks Jeremy.

As for the general "if" understanding, I think I get it.
That's certainly clearer than the datasheet west.
No doubt my knowlege will come to the test soon enough.
Code:
if alband understands then all is well
else read through this again
endif
 

hippy

Ex-Staff (retired)
If you want a one line IF-THEN <statement> rather than IF-THEN <label> you need to write it as -

If pin1 = 1 Then : Let b1 = 1 : End If

That's the same as Jeremy's example with the colons acting as 'new line' separators.
 

westaust55

Moderator
new line separators

While I have seen "new line" separators used a few times in posts, and know of their application in programming from past experience, I have not spied anything about them in PICAXE Manual 2.

Colons are covered as a requirement after a label name at the point a label is defined (but not where the call is made in a command).

Maybe another item to be added to the manuals . . . . .
 
Top