Android Question One day, "Jump" or "goto" will it be possible ?

Discussion in 'Android Questions' started by DOM85, Jul 9, 2019.

  1. DOM85

    DOM85 Active Member Licensed User

    I come_back with an old but true question about Jump possibility in a program.
    I know that many programers consider that all algorythms can be done with loops.
    But sometime the loops make more complex the code, mainly when it is necessary to exit one of many integrated loops. In this case i use indicators, but it is unreadable to maintain. It is like if a car could only turn left of right but never goto straight.

    In addition, in its machine code, Intel offers all the possibles conditional jumps.
    So, if oneday the B4A compiler could offer only a simple "GOTO" statement, it would be very useful.

    Thank you for your attention,
    Nice regards.
    Syd Wright likes this.
  2. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    I think this is down to how you design your functions and the logical methods in which you handle the conditions it can meet. Goto would be very useful but it is a mindset you are dealing with not the language. I had similar thoughts in the early days of B4A coming from a VB6 development platform, but you do get used to it.
    BillMeyer likes this.
  3. DOM85

    DOM85 Active Member Licensed User

    Thanks for your answer.
    I do not think like you.
    Loops are not the only one organization of your code.
    Many functions need to jump anywhere in the code.
    I am a machine code developper, on PC, microcontrollers, and in the past on mini and main-frames.
    I always saw jumps statements. Hopefully they exists, otherwise no evoluted langages could be possible.
    The "loop" idea is mainly an university concept. (I can say that as i am Docteur en Informatique Fondamentale, and in the past professor at a French university).

    Syd Wright likes this.
  4. sorex

    sorex Expert Licensed User

    you can create a similar method with

    if/then blocks
    if/then statement + subroutine call + return
    if/then in a loop combined with single or multiple continues

    it all depends on the routine ofcourse.
    BillMeyer likes this.
  5. MarkusR

    MarkusR Well-Known Member Licensed User

    jumping inside a Sub would be useful.

    maybe you can use Select Case
  6. LucaMs

    LucaMs Expert Licensed User

    GOTO is deplorable and no longer implemented; but the flow "jumps", not so rarely, where it wants, thanks to so many events :D
    Computersmith64 likes this.
  7. MarkusR

    MarkusR Well-Known Member Licensed User

    B4J test with select case
    #Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #End Region

    Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim Timer1 As Timer

    Dim Goto As Int = 0
    Dim a As Int = 0
    Dim b As Int = 0

    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
        Timer1.Enabled = 
    End Sub

    'Return true to allow the default exceptions handler to handle the uncaught exception.
    Sub Application_Error (Error As Exception, StackTrace As StringAs Boolean
    Return True
    End Sub

    Sub Timer1_Tick
    End Sub

    Sub TheLoop
    Select Goto
    Case 0
    If a=10 Then Goto = 2
    Case 1
    Log("A=" & a)
    Log("B=" & b)
                Goto = 
    Case 2
    If b=10 Then Goto = 1
    End Select
    End Sub
    Frank.G likes this.
  8. techknight

    techknight Well-Known Member Licensed User

    Bingo. That is the difference between a High Level language, and a Low Level Language. You get used to it eventually. There were some "meet in the middle ground" lanuages, or mid-level languages like VB6, or early BASIC that can kinda do both. but as far as I have seen those kind of languages have severe tradeoffs from multi-platform issues to outright syntax/use power of the language itself.

    Whereas C can get pretty close to the hardware, essentially why C hung on so long I guess.

    Anything that needs to be a little lower-level when using high level languages always has wrappers, or native libraries written/compiled in a C environment. For this very reason.

    This language is a runtime level language so its a bit harder to do, but technically you could do GoTos and Jumps, but the runtimes would have hell with it, plus it encourages bad programming. Not to mention the Run Time style systems are abstracted away from everything else so its internal "stack" would have to be set up to do that kind of thing.
    Last edited: Jul 10, 2019
    BillMeyer likes this.
  9. DOM85

    DOM85 Active Member Licensed User

    Jumps inside a sub is not a "bad" programming method.
    The main reason for what B4X has no jump (goto) statement is that it is near a translator to Java, and Java don't offer jumps.
    No experimented programmers (and mainly low level programmer) can say that jump are sign of a bad programming. Otherwise all the processor manufacturers ar bad conceptors, and all the O.S. makers are also bad programmers. The just answer is not too of each method (loop or jump), and each method applied to its appropriated case.
    MarkusR likes this.
  10. techknight

    techknight Well-Known Member Licensed User

    Emphasis in my post, on the word "encourages". NOT enforces! the words I used were very different there. So before you argue against me about bad programming, or bad programmers, please understand my context and wording first.

    Besides, again, you speak of low-level programming which jumps and gotos make sense, not to mention branch to subroutine, branch if not 0, decrements branch if 0, compare, branch if equal, etc. etc. etc...

    but ive seen mid-level languages with GoTo statements that bounce around between subroutines and make spagetti code. Hence, Encourages bad programming. However, you can perform proper programming with those statements.

    I guess technically using anything incorrectly encourages bad programming, but I hope you get my point. Being, I just do not see that stuff used in high level languages.

    Why VB6 used it? No idea, even though it is a run-time based language.
    Last edited: Jul 11, 2019
  11. emexes

    emexes Well-Known Member Licensed User

    If we're going to use analogies, a more accurate one would be:

    like if a car could only keep to the road, and not career off into the bushes

    and similarly:
    ... mostly for programmers to shoot themselves in the foot.

    (which reminds me of this related quote: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg." )

    lol. so true.

  12. emexes

    emexes Well-Known Member Licensed User

    This might lose something in translation, but:

    left of right *is* straight

    (assuming equal angles)

    Reminds me of being a child in Paris, dad was driving and when we got near an intersection there was a lot of yelling in French from my uncle and aunt that I did not understand. Later I asked dad what that was all about and he said: well, your uncle said go left, your aunt said go right, so I went straight ahead, and it seems neither of them were happy with that compromise...
  13. Erel

    Erel Administrator Staff Member Licensed User

    This is incorrect. There are many features in B4X that are not available in Java (such as resumable subs and many others) and there are many features in Java that are not available in B4X.
    BillMeyer and Jmu5667 like this.
  14. keirS

    keirS Well-Known Member Licensed User

    Java does offer jumps. Java has labels and the break and continue statement to jump out of loops. If you are going to have GOTO you also need COMEFROM.
    emexes likes this.
  15. emexes

    emexes Well-Known Member Licensed User

    I didn't know about Java named loop exits (feel free to bring them across to B4A...) but I agree 110% with the need for a complementary COMEFROM.
  16. agraham

    agraham Expert Licensed User

    I've managed to write neat structured code, including Basic, for half a century now without using Goto and see no need for it at all. I fully agree with Dijkstra that GoTo is considered harmful.

    As a sly jibe at Erel's expense I did find some spaghetti code in Basic4ppc (written in C#) that used 'goto' but I forgive him - he was younger then!
    BillMeyer and Jmu5667 like this.
  17. LucaMs

    LucaMs Expert Licensed User

    I still don't understand why the use of GOTO was named "spaghetti programming". A "spaghetto" has its start and its end.
    Also, even when you write an "Else If" you are doing a GOTO, a conditional GOTO but anyway e GOTO.

    No need it but nothing against it.
  18. agraham

    agraham Expert Licensed User

    It was called spaghetti code because it leads to a single level of tangled routes of execution that are hard to follow and hard to debug in comparison to block structured loops of code that enforce clear levels of nesting.
    BillMeyer likes this.
  19. MarkusR

    MarkusR Well-Known Member Licensed User

    in past the only problem with goto gosub was because incorrect stack / misusage. not protected by compiler.
    emexes likes this.
  20. advansis

    advansis Member Licensed User

    I think the real utility of GOTO's could be in validation of data, but I usually use a Try/catch approach, throwing an error when I want to goto away. In other circumstances, I prefer if/elseif/else and select/case. Using gosub is the same as function calling, using goto could be done by rewriting the code in more subs and using some flags...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice