If ... Then Gosub <> If ... Then Goto?


Closed Thread
Results 1 to 7 of 7
  1. #1
    oldtoddler's Avatar
    oldtoddler Guest

    Default If ... Then Gosub <> If ... Then Goto?

    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

  2. #2
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Archilochus; - 26th February 2006 at 18:26.

  3. #3
    oldtoddler's Avatar
    oldtoddler Guest


    Did you find this post helpful? Yes | No

    Wink reducing line count by single line if..then..gosub..:endif

    Quote Originally Posted by Archilochus

    ... 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

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    oldtoddler's Avatar
    oldtoddler Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    Quote Originally Posted by Melanie

    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
    Last edited by oldtoddler; - 27th February 2006 at 12:36.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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...
    Code:
    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?...

  7. #7
    oldtoddler's Avatar
    oldtoddler Guest


    Did you find this post helpful? Yes | No

    Thumbs up

    Quote Originally Posted by Melanie
    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> --
    Code:
      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... :-)
    Last edited by oldtoddler; - 27th February 2006 at 12:55.

Similar Threads

  1. Graphic LCD with PICbasic pro
    By amindzo in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 25th November 2012, 12:45
  2. Making a menu
    By chrisshortys in forum mel PIC BASIC Pro
    Replies: 36
    Last Post: - 12th November 2008, 20:54
  3. Problems with RC2 and RC3
    By Christopher4187 in forum General
    Replies: 11
    Last Post: - 29th May 2006, 18:19
  4. Output PIC module
    By freelancebee in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th September 2005, 21:10
  5. ds1307 from f877 to f452 not work
    By microkam in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th July 2005, 01:02

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts