PDA

View Full Version : If ... Then Gosub <> If ... Then Goto?



oldtoddler
- 26th February 2006, 15:23
Newbie question once more.. Why cannot I write like:

LCD_Present VAR PORTA.0
IF LCD_Present Gosub LCD_Subroutine : ENDIF
_____________^^^

I know I can do it by:
IF LCD_Present=1 THEN
Gosub LCD_Subroutine
ENDIF

But it is two extra lines in code.. IMHO
Does it have to be If..Then goto (label)


/MRa

Archilochus
- 26th February 2006, 17:09
Well, in newer versions of PBPro you can write:

IF LCD_Present THEN (label) ; If LCD_Present is more than 0, do something at "label"

No need for the "GOTO" or the "=1"
Don't know which PBP versions support this, of if it will work in PBC

EDIT
oopps didn't realize that you were asking about gosub - not goto!
What if you used the IF-THEN-GOTO, and after the code at the GOTO label you used another GOTO to come back where you were? I guess you'd still have to type the extra lines though.

EndEDIT

oldtoddler
- 27th February 2006, 06:02
... oopps didn't realize that you were asking about gosub - not goto!
What if you used the IF-THEN-GOTO, and after the code at the GOTO label you used another GOTO to come back where you were? I guess you'd still have to type the extra lines though.

EndEDIT

Yep, that's the point (gosub). Since lcd-routines are so SLOW, I would like to use lcd only for diagnostic purposes, when the speed is not so important. There would be no way to know where to return, since there would be 10's of possible (unlabeled) places where the lcd-subroutine needs to be called from.. Of course I could (and maybe I should) use another PIC for communicating and buffering dataIO with outside world (and LCD)...

I dunno - should I put this If..then..Gosub..:Endif into wish-list or not.. It does not matter if you have less than hundred lines of code, but when the line count is more than few hundreds, one starts to write the code in such way it would be as easy to "read" afterwards as possible..
What do ya say folks?

/MRa

Melanie
- 27th February 2006, 08:40
There is no problem with a GOSUB in an IF...THEN... statement (as long as you're using PICBasic Pro... however your syntax is wrong...

Turning to your original post...

IF LCD_Present=1 THEN
Gosub LCD_Subroutine
ENDIF

This is correct... but....

IF LCD_Present Gosub LCD_Subroutine : ENDIF

...is NOT correct. You do not need the ENDIF statement in this case and you DO need a THEN inserted... making a correct statement as...

IF LCD_Present=1 THEN Gosub LCD_Subroutine

Finally... in the two examples above, just because you are using an extra line with an ENDIF statement in one of them, does not generate any extra code when you compile. The ENDIF is really a directive for the compiler that tells it you have a multi-line IF...THEN block rather than a one-liner. If you write your code all on one line or in a block terminated with an ENDIF, the resultant compiled code will be the same. The ability to create large blocked IF THEN statements (terminated with an ENDIF) is one of the most useful features of PBP over PBC. Remember there is no code penalty when using ENDIF.

oldtoddler
- 27th February 2006, 11:19
IF LCD_Present=1 THEN Gosub LCD_Subroutine



Thnx Melanie, yep this is where I did it wrong.. o:-)
This should be mentioned in user manual-right?.. ;-)

The final binary-code size might be same, but PBP code will be shorter that way and much easier to read...

I've used a technique where I write "ENDIF 'short comment" same time as I write corresponding IF statement, in order to keep track with loops. Otherwise it can be difficult to see which ENDIF goes to which IF statement.. this will be now even more important, since one cannot use word counter to match IF and ENDIF count..

Thnx agn, C'ya!

/MRa

Melanie
- 27th February 2006, 11:28
To keep track of IF...ENDIF, or indeed any other block statements which themselves could be nested (such as FOR...NEXT, or WHILE...WEND) it's convenient to indent the code for every block statement. The compiler ignores surplus indents and it makes it more human readable when looking at the code. Example...


Start:
'
' Set Dual-Colour LED
' -------------------
If PowerOn=1 then
High PowerOnLEDGreen
Low PowerOnLEDRed
If BatteryLow=0 then
High PowerOnLEDRed
Low PowerOnLEDGreen
endif
endif
Gosub ScanKeyboard
Goto DoCleverStuff

This way you can easily track the preceeding IF statement associated with any corresponding ENDIF by just following the indented block.

Why is Yoda wearing rubber gloves whilst doing hand-puppet Rabbit impressions?...

oldtoddler
- 27th February 2006, 11:52
To keep track of IF...ENDIF, or indeed any other block statements which themselves could be nested (such as FOR...NEXT, or WHILE...WEND) it's convenient to indent the code for every block statement.

This way you can easily track the preceeding IF statement associated with any corresponding ENDIF by just following the indented block.



Yep, this is how I do it.. I use only one space instead of tab.. way too many nested loops and whiles to fit in one line.. there you can find 5.. Wonder if there is any limit for loop levels?

-->8-- <klipeti> --


IF Mode then GoSub lcd ' | |
ADCIN AN3,IM ' | |
IF IM < 512 Then ' Virta ei vielä yli 0A | | if_11
ADCIN AN1,VM ' Mitataan jännite | | |
IF VM > Vp90 Then ' Jos yli +270V (90% * 300V) | | | if_6
Reg=1 ' Regulaattori IGBT päälle | | | |
wr_fet=0 ' | | | |
PULSOUT WR_FET,latch ' | | | |
' | | | |
Ikkunassa=1 ' | | | |
While Ikkunassa ' | | | | we_7
IF Mode then GoSub lcd ' | | | | |
ADCIN AN1,VM ' | | | | |


> Why is Yoda wearing rubber gloves whilst doing hand-puppet Rabbit impressions?...

Well.. Maybe it has something to do with climate change.. o:-)
As u know, Yoda comes from clean-air environment and he was fotographed in L.A... :-)