'Conditional Branching - 'GoTo' Kluge ? - page 2

 
RaptorUK:
I used GOTOs back in the BBC Basic days . . . I even used BBC BASIC in my full time job for approx 7 years . . . even BBC Basic had GOSUB and RETURN so that subroutines (functions) could be used . . . why would you ever want to use anything that resembles a GOTo in MQL4 ? perhaps you can explain your need ?

Hi Raptor,

As you said yourself, you used such things yourself for years and thus are aware of the myriad of situations that it can be utilized that are very handy. The 'GoSub' and 'Return' functions are basically the same idea as the 'GoTO' that I am referring to. Though with the GoSub/RETURN functions, one has to return to the same point in the calling program. Unlike 'GoTo' which doesn't that can create very messed up nightmares. Though I guess that Include, Library and .dll file utilization capacity in MQL are similar to this. But this doesn't allow for the kind of control one can get with GoSub/RETURN etc

Many iterations of programming for computerized machining contain the GoSub/RETURN pairs as well and it is of considerable usefulness in that environment. Allows for a high degree of modulization and reusing numerous standard 'canned cycles' as well as custom and specialized functions over and over again in numerous programs. Don't have to continually 'reinvent' the wheel' every time one is needed. When I started doing contract computerized machining programming, I was faced with over 2,000 different CNC controllers: all with their own variations in programming )< 8) This forced me to utilize very expensive CAD-CAM software which is to bad. I could often write considerably better and more efficient ones myself that produced better results in less cycle time with better tool wear than the software would generate.

 

GOSUB/RETURN is the same as using functions in MQL4

I did a little CNC programming many years ago . . . AeroSpace work mainly.

 
FourX:

I don't think C has one does it?

Yes it does. It is in K & R second edition page 65. They SAY they do not use it in the book, then give examples of it on the next page!

 

I think what we are trying to emulate here is

dog:

cat:

mouse:

   if( bizarre_condition1 ){
      goto cat;
   }

   if( bizarre_condition2 ){
      goto mouse;
   }

   goto dog:

Which can be done like this ...

while(true){
   
   // dog:
   if( GOTOstate == DOG ){
   }
   
   // cat:
   if( GOTOstate == DOG || GOTOstate== CAT ){
   }
   
   // mouse:
   //no test needed here, just do MOUSE stuff
   
   
   if( bizarre_condition1 ){
      GOTOstate= CAT;
      continue;
   }

   if( bizarre_condition2 ){
      GOTOstate= MOUSE;
      continue;
   }
   
   GOTOstate = DOG;
}
   

but not very elegantly, especially for lots of labels. A better way is

while(true){
   
   switch(GOTOstate){
      case DOG:
         // do DOG stuff
         // break;   // do NOT use the break so you get deliberate FALL-THROUGH
      case CAT:
         // do CAT stuff
         // break;   // do NOT use the break so you get deliberate FALL-THROUGH
      case MOUSE:
         // do MOUSE stuff
         break;
      default:
         break;     
   }
   
   if( bizarre_condition1 ){
      GOTOstate= CAT;
      continue;
   }

   if( bizarre_condition2 ){
      GOTOstate= MOUSE;
      continue;
   }
}

but you really need to put function calls in the switch statement otherwise if the switch goes over several pages of code you cannot see the structure of the code.

 
FourX:

Hi SDC,

As with most programming, MQL4 is virtually all 'Functions' that other than jumping over some lines, doesn't have a true 'GoTo' function that I am trying to figure out how to do within a MQL4 EA So I'm not sure what you are 'agreeing with' and what you mean and are referring to in the context of this search? Please expand and explain. Not being argumentative. It sounds like you have something in mind and I'd like to know what it is and how it might be beneficial to us.


Hi FourX I misread the previous posts I thought someone had already said what I meant is the function call superceeds the idea of GoTo and improves the workability of it not least for the fact it is easier to read code afterwards when it is organised into functions than it is to follow GoTo's, and like dabbler showed with his example, multiway branching using the switch can achieve the same thing as multiple GoTo.

 
dabbler:

I think what we are trying to emulate here is

Which can be done like this ...

but not very elegantly, especially for lots of labels. A better way is

but you really need to put function calls in the switch statement otherwise if the switch goes over several pages of code you cannot see the structure of the code.


Hi Dabbler,

A true 'GoTo' statement/function allows one to jump to ANY place within the program, not just skip over the next (couple of) Expressions / Statements. This allows for things such as re-utilizing the same functions that are needed in more than one instance in a program.

Hence both its flexibility and convenience. Which as I've mentioned is a double edged sword as this can make it very easy to get into trouble with it and make for programs that are nightmares.

Does MQL5 have anything like a 'GoSub/RETURN', 'GoTo' etc ? What bout Visual Basic, Java, PHP etc ?

 
RaptorUK:

GOSUB/RETURN is the same as using functions in MQL4

I did a little CNC programming many years ago . . . AeroSpace work mainly.

As far as I have been able to figure out so far Raptor, their is nothing such as GoSub / Return commands or the ability to make such a function in MQL4. Hence my trying to figure out a kluge in MQL4 to carry out this capacity.

 
dabbler:

I think what we are trying to emulate here is

Which can be done like this ...

but not very elegantly, especially for lots of labels. A better way is

but you really need to put function calls in the switch statement otherwise if the switch goes over several pages of code you cannot see the structure of the code.

MQL4 Reference - Basics - Syntax of MQL4 is much a C-like syntax, apart from some features:

  • no address arithmetic;

  • no operator do ... while;

  • no operator goto ...;

  • no operation of [condition]?[expression 1]:[expression 2];

  • no compound data types (structures);

  • complex assignments are impossible; for example, val1=val2=0; arr[i++]=val; cond=(cnt=OrdersTotal)>0; etc.;

  • calculation of a logical expression is always completed, never early terminated.

 

Hi 1withZachy,

In essence this seems that it is MQL4 SOP that really just utilizes a standard or custom function and then 'returns' the result to the function as MQL4 does. Though by the flow diagram their may be a subtle distinction that I am missing out on, but I don't think so. Alas the documentation within the provided sample code: 'CallFuntion.mq4' is in Russian which I am not able to comprehend at all.

 

Some excerpts from an interesting artical about GoTo on Wikipedia:

Many languages support the goto statement, and many do not. In Java, goto is a reserved word, but is unusable.[1][2] In PHP there was no native support for goto until version 5.3 (libraries were available to emulate its functionality).[3]

The structured program theorem proved that the goto statement is not necessary to write programs; some combination of the three programming constructs of sequence, selection/choice, and repetition/iteration are sufficient for any computation that can be performed by a Turing machine.

The 1960s and 1970s saw computer scientists move away from GOTO statements in favor of the "structured programming" paradigm. Some[which?] programming style coding standards prohibit use of GOTO statements, particularly in view of the aforementioned structured program theorem. The Böhm-Jacopini proof did not settle the question of whether to adopt structured programming for software development, partly because the construction was more likely to obscure a program than to improve it.

Probably the most famous criticism of GOTO is a 1968 letter by Edsger Dijkstra called Go To Statement Considered Harmful.[5] In that letter Dijkstra argued that unrestricted GOTO statements should be abolished from higher-level languages because they complicated the task of analyzing and verifying the correctness of programs (particularly those involving loops)

An alternative viewpoint is presented in Donald Knuth's Structured Programming with go to Statements[6] which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.