the 'IF then' Command

Solitaire

New Member
Ok, this has become the biggest issue so far for me 2 over come. The way the currect PBASIC 'if... then' connad works is:

IF "expression" THEN "stage1"
ELSE
"stage2"

Now, I need some way of being able 2 set a variable if the IF statment is true. That isn't hard, however with about 60 IF statements all with different required outputs, it is not easy to make another section for each stage.

I need some way of making a IF statement the wroks like this:

IF pin0 = 1 then { b1 = 1 }

I no i can jsut make a new "stage" for the setting however, as i said, with 60 or so of them i have little room left for my other programs. I am running short of room on a 600 line pic as it is, and i cant connect an EPROM. I could, if worst comes 2 worst, have 2 pics yet this comes out of the scope of my project.

Any ideas?

Joe Valentine, aka, Solitaire

 

hippy

Ex-Staff (retired)
Not necessarily !

In the original example 'IF pin0 = 1 THEN { b1 = 1 }', if pin0 is zero, then b1 would retain its value prior to the IF statement; it would not be set to zero.

As the pin0 variable always returns zero or one, another means of setting b1 to the value of pin0 ( if that is what is required ) is simply to use ...

- b1 = pin0

That can also be used for any other pin or variable combination without getting into complex bit masking and shifting.

The actual solution to the problem depends very much upon what the program needs to do. It may be better to think in terms of when not to set a variable, hence 'IF pin0 = 1 THEN { b1 = 1 }' becomes ...

- IF pin0 <b>&lt;&gt; </b> 1 THEN Pin0IsNot1
- b1 = 1
- Pin0IsNot1:

Admittedly it's still not really that elegant. Depending upon what your code actually needs to do you may be able to use LOOKUP or BRANCH to good effect, maybe even re-writing the code in a different way entirely, but it would very much depend upon what you are trying to do as a whole.

Keep in mind that if you could use ...

- IF <i>condition </i> THEN
- <i>trueStatement </i>
- ELSE
- <i>falseStatement </i>
- END IF

it would still probably compile down to the much more verbose PICAXE code ...

- IF <i>condition </i> THEN DoTrueStatement
- GOTO DoFalseStatement
- DoTrueStatement:
- <i>trueStatement </i>
- GOTO Done
- DoFalseStatement:
- <i>falseStatement </i>
- Done:

Just being able to write the program in a much more cleaner and compact way doesn't necessarily mean that the resulting program code will be any shorter.
 

Solitaire

New Member
Is it not the number of lines there are in ur acutaly program, the number of lines used on the pixace? OOO good point
When it is converted 2 Assembler it may all change. Hmm iwil have a look :)
 
Top