Android Question Conditional and unconditional jumps in B4X

DOM85

Active Member
Licensed User
Longtime User
Hello,
All processors have conditional and unconditional branch instructions (Je, Jne, Jl, Jmp, ...), and operating systems use them a lot.
"Loops, Do..." are a programming tool, conditional and unconditional branches too.
Often, loops require a lot of flags to exit, when a simple Goto or jump would simplify both processing and understanding during maintenance.

Is implementing these instructions on B4X at least inside a Sub function really too difficult?

Thank you.
 

BlueVision

Well-Known Member
Licensed User
Longtime User
Your question is a bit vague.
B4X is categorised as a procedure-oriented language.

There are many ways to generate conditional or unconditional programme branches, many of which are also known from other BASIC dialects.

IF THEN
IIF THEN
SELECT CASE
(to name just three)

The only difference is that you first assign a procedural code to a control element or an event (programme start) and execute the corresponding actions in this procedure. Depending on the result, another procedure is then called and so on...

The previously feared spaghetti code generated by GOTO statements can also occur here (even without GOTO). This usually happens if the programme is not preceded by a well thought-out construct, i.e. if you are simply programming at random.

And yes, you have to use flags for leaving a loop.
Here a (dirty hack) example for leaving a loop:

Exiting a loop:
Do While event
    If condition Then Exit
Loop

condition could be a flag, often a boolean variable is used (true/false)
don't forget to reset the boolean variable afterwards
 
Upvote 0

DOM85

Active Member
Licensed User
Longtime User
Your question is a bit vague.
B4X is categorised as a procedure-oriented language.

There are many ways to generate conditional or unconditional programme branches, many of which are also known from other BASIC dialects.

IF THEN
IIF THEN
SELECT CASE
(to name just three)

The only difference is that you first assign a procedural code to a control element or an event (programme start) and execute the corresponding actions in this procedure. Depending on the result, another procedure is then called and so on...

The previously feared spaghetti code generated by GOTO statements can also occur here (even without GOTO). This usually happens if the programme is not preceded by a well thought-out construct, i.e. if you are simply programming at random.

And yes, you have to use flags for leaving a loop.
Here a (dirty hack) example for leaving a loop:

Exiting a loop:
Do While event
    If condition Then Exit
Loop

condition could be a flag, often a boolean variable is used (true/false)
don't forget to reset the boolean variable afterwards
Thank you for your reply.
However no, it is not spaghetti programming when using jumps (smile).
All the processors use them ! Maybe you know that.
I wrote thousands (maybe near 50 !) of big programs in machine code (while creating a compiler and many system products) and i can say thet it is often very useful and clear to maintain.

However you are right about your methods, but not only.
Thanks.

This question is for B4X developper.
 
Upvote 0

BlueVision

Well-Known Member
Licensed User
Longtime User
Hmm, perhaps we've missed the point a little.

A direct jump is of course also possible within B4X. But this is implemented far more elegantly in B4X (as in all higher programming languages). You can branch to a required procedure at any time in the B4X programme code. In principle, this corresponds to a GOTO, or rather a GOSUB. The difference of a procedure in contrast to an unconditional jump (GOTO or GOSUB) is the possible parameter transfer for the return jump at the end of the procedure. Of course, this is not always necessary, but it is the great advantage of a procedure (a private or public sub). So there is no need of a GOTO.

This question is for B4X developper.

I don't get the point of this statement.
 
Upvote 0

DOM85

Active Member
Licensed User
Longtime User
Hmm, perhaps we've missed the point a little.

A direct jump is of course also possible within B4X. But this is implemented far more elegantly in B4X (as in all higher programming languages). You can branch to a required procedure at any time in the B4X programme code. In principle, this corresponds to a GOTO, or rather a GOSUB. The difference of a procedure in contrast to an unconditional jump (GOTO or GOSUB) is the possible parameter transfer for the return jump at the end of the procedure. Of course, this is not always necessary, but it is the great advantage of a procedure (a private or public sub). So there is no need of a GOTO.



I don't get the point of this statement.
I read your nice answer. Thank you.
I add that one important difference between a procedure and a conditional jump is that in this second case the return is not desired.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Unconditional jump instructions like GOTO are considered deprecated for several reasons:

  1. Complexity of control flow: Using GOTO makes program control flow much more complex and less clear, leading to code that is difficult to read, understand, and maintain over time.
  2. Debugging difficulties: GOTO can make debugging harder since unconditional jumps can complicate tracking program execution in a linear fashion.
  3. Lack of structured programming: The use of GOTO violates the principles of structured programming, which promotes control flow constructs like if-else statements, loops, and subroutines to improve code readability and maintainability.
  4. Potential errors: Overuse of GOTO can easily lead to programming errors, such as infinite loops or situations where parts of the code are not executed as expected.
In modern programming, many languages have removed or significantly restricted the use of GOTO to encourage safer and more structured programming practices. However, in certain very specific and controlled cases, some languages still support similar constructs.

One of the most famous critics of GOTO was Edsger Dijkstra, a pioneer in computer science. In his famous 1968 article titled "Go To Statement Considered Harmful", published in the Communications of the ACM journal, Dijkstra strongly criticized the indiscriminate use of GOTO, emphasizing how it can compromise code clarity and debugging ease. This article had a significant impact on the widespread adoption of structured programming practices.

In summary, abandoning GOTO has been an important step toward writing clearer, safer, and more maintainable code in modern programming languages.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
Say that to Intel, Motorola, Microchip, Texas, Dell, IBM, and so on. Maybe they are less specialist 🤣
We are talking different levels here: low level languages have simple instructions, while high level languages offer structured constructs.
What it your background? Which languages have you worked with?
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I read your nice answer. Thank you.
I add that one important difference between a procedure and a conditional jump is that in this second case the return is not desired.
An object-oriented language program should not be structured the same as a procedural-driven language (e.g. COBOL, Assembly, etc.). If you're still thinking in terms of GOTO's then you should see if your solution could be optimized or otherwise adapted to an object-oriented approach.

Having said that, you can mimic the "return not desired" behavior with the Return statement.
B4X:
Select Case MyVar
    Case 1
        MyOtherSub
         Return
    Case 2
         MyOtherOtherSub
         Return
End Select
Unless you're speaking to something such as "clear the call stack and begin executing this procedure" in which case, this really is not a feature in most obect-oriented languages.
 
Upvote 0

QSerg

Member
I'm sure Erel would reply that he will never add GOTO, and rightly so.
You may call me a retrograde or even Old Fart, but because I started with Fortran I fail to see anything wrong with GOTO. Yes it can be abused like anything else, but from time to time it allows create simple and neat code. YMMV.
 
Upvote 0
Top