Confused NuB: Series of ‘if’ statements & ‘return’ - page 2

 
7bit:
return will immediately exit the function and return to where the function was called.

If you now ask what a function is, then maybe you shoud search the web for some introductory programming tutorial (on whatever language you like, it doesn't matter, preferable something other than mql) to learn the very basics of what a program is, of what parts and structures it can consist, etc.

Since you are an absolute newbie I will now try to form you, to massively influence the rest of your programming life with the following few rules:

there are two possible ways to write the if construct, the short form often leads to confusion.

and the longer form:
in the first case there are the { and } missing, this is an exception from the general rule, blocks are always enclused in {}. In the above case it will only execute the next command (or do the next if). Please ALWAYS use the second form, even when only one line is to be executed by the if. The curly braces simply are more clear and obvious and al will be more uniform and the whole program will be easier to read and understand.

The other thing you should practice from the very beginning on is proper indentation. Always. Your code has to look nice. Its structure should already be clear from looking at the formatting alone. Look up the One-True-Brace-Style on wikipedia. 1TBS style for formatting is the only one true style. Metaquotes people are doing it wrong and therefore I hate them for keeping newbies off from proper formatting. Half of the n00bs are writing a complete mess formatting-wise. Remember: there is only one true style!

And now please start reading a programming book. Learning Java would be a good Idea, its syntax is very similar to mql4, but it is easier to learn. After you have done this you can try learning mql4.



I don't need math 101 thanks.

The example I posted example, I wasn't worried about having the syntax correct but was focusing on the aspects of my query. I have however gone in and added another consecutive 'if' statement and added braces to it and I understand how they form groups of program blocks.

I understand the influence that the indentation can and does make on the structure of it makes visually clearer very quickly.

I'm all for learning and forming good programming habits and techniques from the start as I know it will pay a LOT of dividends down the line. I'll check out 1TBS style thanks.

I won't be learning an intermediate language, MQL4 it is.

So in my first example where all of the 4 'if' statements are true, which of the 4 'if' statements would it go TO when it encounters the 'return' statement ?

Likewise am I correct that if one of the preceding 'if' statements is false that it will not carry out the 'Action' OR will it just skip the next 'if' statement and proceed with the 2 line of code following the 'if' statement that is false?

If the 'action' gets ruled out because of a 'false' 'if' statements and then will it move on to the second block of programming ?

THESE are the things that I'm asking about as they are the ones that I need clarification on and need to know them definitively.

 
jjc:

Definitely a matter of personal (or team) preference. The weakness of all the Wikipedia examples is that they don't include any code comments. My personal view is that the readability of Allman/Whitesmiths tends to be more adversely affected than 1TBS by the addition of comments, particularly blocks of multi-line comment.

I am 'documenting' my code extensively. Especially at this point in time when I am just starting to learn MQL4

 
7bit:


Hi 7Bit,
I agree that the indentations make the visual layout very quick and easy to spot the program structure and have now included them. I do so in my programming.
For similar reasons I am going to keep ALL of the braces 'paired up' on the left hand side nested with indentations as well as then I can very readily see what and where the pairs are and find a missing or extra brace if I have one.
But this does not answer any of my SPECIFIC questions in the example that I preseented which is WHAT I NEED to KNOW!
Please view it again and answer the questions contained in it.
Thanks 7Bit
(Why 7 bit? Don't want to be 'Octal'? LoL)
 
FourX:
Please view it again and answer the questions contained in it.

FourX, personally I didn't notice your questions in the code until u mentioned them. I am guessing others didn't either. Many programmers have a tendency to ignore comments in code unless they really need to read them...


//-- Block A --------------------
  {
   if(Condition_1 = True)
    if(Condition_2 = True)
     if(Condition_3 = True)
      if(Condition_4 = True)
      {
       Action ; // <== This WILL be carried out = True
        Return; // To Where? To the first 'if(Condition_1=True)' in Block A? Or the start of Block B or????
      }
  }
//-----------------------------------

//-- Block B------
These 2 blocks are either in a custom function or in one of the special functions (init(), deinit(), start()). The 'return' operator will terminate this function (where the code is) and return control to the function it was called from.
  1. In case the code is in a custom function, program will continue running from the next statement after the function call (in the function it was called from).
  2. In case of special functions, program control will be passed back to the Terminal.
In either case, it won't return to the start of block A or block B since they are both contained in this function (which is terminated).

//---- Block A -----------------------------
   {
    if(Condition_1 = True)
     if(Condition_2 = False)
      if(Condition_3 = True)
       if(Condition_4 = True)
        {
         Action ; // This 'Action' will NOT be carried out? = True ?          
          Return; // Move onto Block B  OR go back to the first(?) 'if(Condition_1=True)' or ????
        }
   }

//-----------------------------------

//-- Block B------

The problem with the question is that u r using '=' instead of '==' as mentioned before in this thread... I don't think that would even compile. If u meant that the entire expression in the second 'if' is false - in that case program run will continue right after the 2nd 'if', so the 3rd and 4th 'if' would be skipped (and so would Action and 'Return').


I really recommend u read this thoroughly -> https://book.mql4.com/basics/functions.

 
Gordon wrote >>

One comment: they are equivalent in logic, but not in speed. MQL4, unlike many other languages, calculates expressions completely and does not use any short estimates. So for example - if the conditions are functions then in the 2nd case all 3 functions will be evaluated regardless of their return, whereas in the 1st case it depends on their return value (if condition1 evaluates false then the other 2 conditions won't be evaluated).

Hi Gordon et al,

I understand what you mean when you initially say that regardless, all functions will be processed even if the statements have been ruled out by the logic. But then you seem to contradict yourself with:

'in the 1st case it depends on their return value (if condition1 evaluates false then the other 2 conditions won't be evaluated'

Are you referring to each 'case' being each line of code (in this case all 'if' statements) being an 'statement' here as opposed to they way I have it broken up into 2 separate local blocks of program groups?

I know this doesn't make any difference as far as the logic evaluation and the resulting path that the program takes, but I am interested in writing efficient code that runs as fast as possible.

 
I appreciate that you folks that are already knowledgeable and experienced are debating the finer points of this, but that isn't answering my question or helping me!

If any of the 4 ''if' statements' as in 'Case 2' are 'false', does it just go on past the 'false' 'if' statement? || does it branch off and leaves the entire local 'Block A' and jumps to 'Block 'B'' OR (||) does it just jump over the 'false 'statement'' and continue processing the rest of the statements in the local 'Block A' immediately following the 'false' code line?

I'm trying to find out EXACTLY where the 'return' statement will go to in example 1 & 2. I have already read and know that 'it goes back to the program that called it'. But in my example of 4 if statements in a row, that is ambiguous to me as it could be anyone of them!

FYI: I fully recognize that I don't have any specific programming code in the 'second block' of programming as it is not relevant to my query and understanding so PLEASE don't leave comments about the 'code' in 'Block B' being incorrect!
PLEASE answer the specific questions using my example.


 
DxdCn:

=
do not is
==

-----------------------------------------------------
????? why use = for condition ?
If (Condition_1 = True)

If (Condition_2 = True)

If (Condition_3 = True)

I'm well aware of that thank you. None of it is 'valid' MQL4 code and would not run at all. I didn't get into specifics as even WITH the '==' instead of just one '=' what I have there is still NOT valid MQL4 code. The syntax is irrelevant to the example, it is the LOGIC/program flow/route that I am inquiring about.

However I HAVE changed the 'true' statements to the syntactically correct '==' !
I assumed that everyone would readily see that it isn't 'valid MQL4' code and wouldn't worry about it as my question addresses the logic && program flow.
 
FourX:
Gordon wrote >>

Hi Gordon et al,

I understand what you mean when you initially say that regardless, all functions will be processed even if the statements have been ruled out by the logic. But then you seem to contradict yourself with:

'in the 1st case it depends on their return value (if condition1 evaluates false then the other 2 conditions won't be evaluated'

Are you referring to each 'case' being each line of code (in this case all 'if' statements) being an 'statement' here as opposed to they way I have it broken up into 2 separate local blocks of program groups?

I know this doesn't make any difference as far as the logic evaluation and the resulting path that the program takes, but I am interested in writing efficient code that runs as fast as possible.

I was reffering to 7bit's example and not to yours (I even quoted his example in that post). The 2nd case is this one:

if (condition1 && condition2 && condition2) {
   doSomething();
}
doSomethingAlways()

Here all conditions will be evaluated first and the result will be used as a condition for the 'if' operator. On the other hand, in the following case (which is the 1st case in 7bit's example):

if (condition1) {
   if (condition2) {
      if (condition3) {
         doSomething();
      }
   }
}

condition1 will be evaluated first. If it's true than condition2 is evaluated, otherwise program run continues after the first 'if' closing braces... etc.

 
FourX:

If any of the 4 ''if' statements' as in 'Case 2' are 'false', does it just go on past the 'false' 'if' statement? || does it branch off and leaves the entire local 'Block A' and jumps to 'Block 'B'' OR (||) does it just jump over the 'false 'statement'' and continue processing the rest of the statements in the local 'Block A' immediately following the 'false' code line?

The 'if' statements are evaluated by the order they appear in your code. The first one that evaluates to false will cause program run to skip to the end of that 'if' (and all other inner 'if' statements will be skipped as well).


I'm trying to find out EXACTLY where the 'return' statement will go to in example 1 & 2. I have already read and know that 'it goes back to the program that called it'. But in my example of 4 if statements in a row, that is ambiguous to me as it could be anyone of them!

As I said before, it goes back to the function that called the function that this code is in. An 'if' operator is not a function... so it does not go back to any of them.

 
FourX:which of the 4 'if' statements would it go TO when it encounters the 'return' statement ?
The return statement has absolutely nothing to do with the if statements.


void foo(){
   if (a)
      if (b)
         if (c)
            DoThisOnlyIfAllThreeAreTrue();

   // the following will always be executed. 
   // If it fails at any of the ifs it will directly jump here.
   // If all ifs are true it will do the function call and then come here
   return();
}
You simply did not read my answer. I answered the return question in the very forst sentence: It will exit the current function and return to where this function was called. Your example is incomplete, it doesn't contain the whole function definition and it doesn't contain the function call, so the place it will return to is not visible in the part of the code you posted. It is somewhere else. It is where these code was called.

int start(){
   foo();         // <-- call foo() and foo will return to here when it returns
   return(0);
}

void foo(){
   if (a)
      if (b)
         if (c)
            fooBaz();  // <-- fooBaz will only be called if all 3 ifs are true

   // the following will always be executed. 
   // If it fails at any of the ifs it will directly jump here.
   // If all ifs are true it will call fooBaz() and after fooBaz() returns
   // it will also come here.

   return(0); // <-- this will return to wherever foo was called
}

void fooBaz(){
   Print("something has called the FooBaz-function");
   return(0); // <-- this will return to wherever FooBaz was called.
}
  • MT4 will call the special function start()
  • this function will then call (or not call) the function fooBaz().
  • fooBaz will return to where it was called (look up what a call-stack is) in this case it will return to right after the fooBaz() call in foo().
  • foo() itself will then return to start() because it was called from there.
  • start() itself will then return to the MT4 application because it was called from there.
I don't know how to make it any clearer.

All your questions where already answered with my first post. You need to read a programming book if you dont even know what a function is. (Hint: it has nothing to do with math 101, a function in programming is more than that) and if you want to progress as fast as possible then learn another language because in no other language you will progress with your learning so slowly as in mql4!

And if you don't want to learn then i wonder what you are doing here. Otherwise do what the teacher is telling you!
Reason: