General > Technical support

goto command

(1/2) > >>

uknanoman:
Hi Tech
I am just doing a routine where I need to branch off within a custom function using the goto command. I have a register which holds my Sequence number and is updated
according to other factors in the program. This is named as SeqNo and is assigned to a dm[xx] register. Now everything works fine using multiple ifs and goto @xx but the text can get
very long using either if or an elseif variation.  Anyway I tried to use the following line so as to get rid of all the ifs

Goto @SeqNo

But this gave me an error. So then I tried

a$="@"+chr$(SeqNo)
goto a$         

This did not work either so any help would be appreciated.


These lines below show what I am trying to reduce if possible.

if SeqNo = 1
goto @1
endif

if SeqNo = 2
goto @2
endif

if SeqNo = 3
goto @3
endif

if SeqNo = 10
goto @10  '
endif

@1
code for this routine
EXIT

@2
code for this routine
EXIT

@3
code for this routine
EXIT

@10
code for this routine
EXIT

garysdickinson:
I have been writing code for the TRI PLCs for nearly 20 years and have not found a use for the GOTO statement.

I would use the following coding structure for your code:

if SeqNo = 1
    ' code for SeqNo 1
elif SeqNo = 2
    ' code for SeqNo 2
elif SeqNo = 3
    ' code for SeqNo 3
elif SeqNo = 10
    ' code for SeqNo 10
else
    ' code if the SeqNo is not not 1,2,3, or 10 ...
endif

I think that this approach is easier to read and maintain.  Note that "elif" is identical to "elseif" keyword. I just like the shorter version.

Regards,

Gary Dickinson

support:
The destination xxx for GOTO  @xxx is determined at compile time and not at run time. Hence it is not possible to assign it a variable.

Gary is right that structured programming can do away with GOTO altogether, although GOTO sometime can be a quick way to get out from deep inside a nested structure to another location. However, over-using GOTO can easily lead to so called "spaghetti codes" that can be error prone and difficult to debug so should be used sparingly.

uknanoman:
Hi Gary
          Thanks for the helpful response. Yes I understand your approach and it was a way I originally considered and the reason I was looking for a more simplified textual layout was because within the code sections there are / would be, many more nested ifs. In my example I only showed if branches for 1,2,3 and 10 but in reality there will be about 35 - 40 branches. So my thinking was that by using the goto command I would achieve two things. Firstly all the multiple if / elif / elseif chain would be reduced to one simple line that just reads - goto @xx where xx represents the sequence number. Secondly all the code for each section would then be headed with a @xx and this in my mind grouped in simple chunks of text, each sequentially following the other until coming to the last ‘@40’ label. I realise you get this as I have always been impressed with your many posts over the years so I am really only explaining this in more detail for others who may be wondering what an why I am wittering on about. As so often happens, the process of posting a tech question makes one re-evaluate ones thought process. With this in mind it may be better for me to call up say 40 custom functions from the if elseif chain and segregate my code by custom function rather than by a goto label. Any thoughts ?
Once again thanks for your help
All the best
Pete

uknanoman:
Hi support
               Thanks for the clarification, I was typing to Gary whilst this came in so I appreciate the detail so I will now proceed as guided by Gary and the info you have now supplied. My response to Gary includes one new way I shall try.
Many thanks to all
Pete

Navigation

[0] Message Index

[#] Next page

Go to full version