PDA

View Full Version : Question about If Then



Alektric
- 19th January 2009, 13:36
I have always found the If Then command in PicBasic very limiting:


5.13. IF..THEN

IF Comp { AND/OR Comp } THEN Label

Performs one or more comparisons. Each Comp term can relate a variable to a constant or other variable and must be in the following form:

Var ( < | <= | = | <> | >= | > ) Value

All comparisons are unsigned since PBC only supports unsigned types. A variable must occur on the left.

The THEN in an IF..THEN is essentially a GOTO. If the condition is true, the program will GOTO the label after the THEN. If the condition evaluates to false, the program will continue at the next line after the IF..THEN. Another statement may not be placed after the THEN, it must be a label

Yet I have seen posts of code written in a similar style to C:
If B0 = 0 Then
(code to run goes here)
Else
(more code)
EndIf

If I try this, my compiler does not like it, telling me that it expected a label after the Then (as stated in the manual).

Can anyone tell me how I can use this style in PicBasic?

Sorry if it's a dumb question and thanks for reading!

Acetronics2
- 19th January 2009, 14:07
hi,

Did you try ???

[code]
If B0 = 0 Then nojump
nojump:

(code to run goes here)
GOTO Jump
Else
(more code)
EndIf
Jump:

[code]

Looks obvious ... but note I'm using PBPro ...

or :

[code]
If B0 = 0 Then turnaround

Else
(more code)
EndIf

BackHome:

.
.
.
.

Turnaround:
(code to run goes here)
GOTO BackHome

[code]

This structure works with assembler ... so it should go too !!!

Alain

Alektric
- 19th January 2009, 14:20
Yes, I have tried that. PB pro does look much better! In PB, Else is seen to be a label and it gives a ": expected" error on the line with Else on it. The method that works is very similar to what you suggested:

If B0 = 1 Then LIGHTON
Low 7
Goto NEXTPART
LIGHTON:
High 7
NEXTPART:
(code continues)

But unlike this most simple example, things start to get complicated (for me) when there are more If statements referring to that variable which makes what should be a single code block get spread out across many jump labels.

So the Else and EndIf commands are used in PB pro? - I'll have to look into it. Thanks for the help.

manta_z
- 18th March 2009, 02:06
Yes, I have tried that. PB pro does look much better! In PB, Else is seen to be a label and it gives a ": expected" error on the line with Else on it. The method that works is very similar to what you suggested:

If B0 = 1 Then LIGHTON
Low 7
Goto NEXTPART
LIGHTON:
High 7
NEXTPART:
(code continues)

But unlike this most simple example, things start to get complicated (for me) when there are more If statements referring to that variable which makes what should be a single code block get spread out across many jump labels.

So the Else and EndIf commands are used in PB pro? - I'll have to look into it. Thanks for the help.

I also have run into this.
PB is not what I was expecting, or sold for that matter.
Nothing works like the examples given.
I even get errors compiling the examples as you state.

I cannot define VAR, CON, INCLUDES, etc. whats up with that?
I'm gonna have to go back to MPLab asm at least it WORKS like I expect!

I thought buying PB was going to help but it has made things more difficult.

All I want to do is read pins and set pins with some if---elses but all I can do is peek and poke. I might as well just do asm if I have to write code in broken assembler...

I am not trying to stir the pot but this is not straight forward as it is shown
in software examples.

Back to asm I go, ugh!!!


-M_Z-

Darrel Taylor
- 18th March 2009, 02:29
Did you buy PBP (PicBasic Pro) or PBC (PicBasic Compiler).

If you got PBP, then all those things will work.

If you took the cheeper version (PBC) ... then you have a LONG nighmare ahead of you.