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


Closed Thread
Results 1 to 7 of 7

Hybrid View

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

  2. #2
    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 11:36.

  3. #3
    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?...

  4. #4
    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 11:55.

Similar Threads

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

Members who have read this thread : 0

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