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

DOM85

Active Member
Licensed User
Longtime User
Hello,
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.
 

Jmu5667

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

DOM85

Active Member
Licensed User
Longtime 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).

Thanks.
 
Upvote 0

sorex

Expert
Licensed User
Longtime 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.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
jumping inside a Sub would be useful.

maybe you can use Select Case
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
B4J test with select case
B4X:
#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.
    MainForm.Show
    
    Timer1.Initialize("Timer1",1)
    Timer1.Enabled = True
    
End Sub

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

Sub Timer1_Tick
    
    TheLoop
    
End Sub

Sub TheLoop
    
    Select Goto
        Case 0
            a=a+1
            If a=10 Then Goto = 2
        Case 1
            Log("A=" & a)
            Log("B=" & b)
            a=0
            b=0
            Goto = 0           
        Case 2
            b=b+1
            If b=10 Then Goto = 1
            
    End Select
    
End Sub
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime 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:
Upvote 0

DOM85

Active Member
Licensed User
Longtime 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.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime 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:
Upvote 0

emexes

Expert
Licensed User
It is like if a car could only turn left of right but never goto straight.
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:
So, if oneday the B4A compiler could offer only a simple "GOTO" statement, it would be very useful
... 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.

 
Upvote 0

emexes

Expert
Licensed User
turn left of right but never goto straight
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...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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.
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.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

emexes

Expert
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.
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.
 
Upvote 0

agraham

Expert
Licensed User
Longtime 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!
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
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!
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.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
in past the only problem with goto gosub was because incorrect stack / misusage. not protected by compiler.
 
Upvote 0

advansis

Active Member
Licensed User
Longtime 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...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…