Confused NuB: Series of ‘if’ statements & ‘return’

 

Confused NuB: Series of ‘if’ statements & ‘return’

I’m having troubles knowing what a couple of basic things do FOR SURE!. Without having these straight, I have no hope of being able to program MQL4.

I'm not sure if one of a series of ‘if’ statements is ‘false’ will the ‘action’ after them in the same block be carried out or not ??

Even less sure about where a ‘Return’ statement takes the program to!

---------------------
I did edit the sample code and added a fourth consecutive 'if' line of code and the Braces and clarified where I think the operation will go to once it gets to the 'return' operator
.
--------------------

Examples below with what I believe to be the correct answer to be in bold :

Thanks for your help!

// +++++++ Case 1 ++++++++++++++++++++++++++++++++++++++

//-- 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------

//================================================================================

// +++++++ Case 2 ++++++++++++++++++++++++++++++++++++++++++++

//---- 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------

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

Thanks for your help! 



 

Are these conditions joined by AND's (&&) or OR's (||)?

 
Hi Joe,
No. I understand that part of it. Each 'if' statement on each line is a simple 'if' statement that is either true or false as indicated.
 
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.

if (foo == bar)
   Print("foo equals bar");

Print("always executed");
and the longer form:
if (foo == bar){
   print("foo equals bar");
   print("another command only when if is true");
}

print ("always executed");
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.
 

Sorry about that. I'm still not following, but someone else probably will, so I'll watch for the answer.

return sends program back to point of the function call if a custom function (i.e. one you design). If it's a special function (init, start, deinit), return sends control back to the terminal (i.e. the special function terminates until called again by the terminal. On an EA, if it's in the start() function, it would be called again on the next tick). So in your example, if Block A is a function, return would take the program back to the part of the program that called it in the first place.

 
if (condition1)
if (condition2)
if (condition3)
doSomething();
doSomethingAlways()

// is equivalent to: (it just looks nicer when indented and is easier to understand)

if (condition1)
   if (condition2)
      if (condition3)
         doSomething();
doSomethingAlways()

// which is equivalent to: (always prefer this form with the braces,
// an opening brace always goes to the end of a line and a closing brace
// to the beginning of the line, to the original indentation level)

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

// and it would also be equivalent to the following: (&& means logical and)

if (condition1 && condition2 && condition2) {
   doSomething();
}
doSomethingAlways()
 
7bit:
if (condition1) {
   if (condition2) {
      if (condition3) {
         doSomething();
      }
   }
}
doSomethingAlways()

// and it would also be equivalent to the following: (&& means logical and)

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

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).

 
7bit:
Look up the One-True-Brace-Style on wikipedia. 1TBS style for formatting is the only one true style.

You are opening a can of worms there... :)

 

=
do not is
==

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

If (Condition_2 = True)

If (Condition_3 = True)

 
gordon wrote >>

You are opening a can of worms there... :)


Maybe!
I'm old-school and use Allman/Whitesmiths
I find it's just clearer when used with progressive indentation, e.g.
  if (data != NULL && res > 0)
    {
    if (comm != NULL && sym < 5)
      {
      if (sys != NULL && dbc != 1)
        {
        if (mark != NULL && kvr == 12)
          {
          JS_DefineProperty(cx, o, "data", 
            STRING_TO_JSVAL(JS_NewStringCopyN(cx, data, res)),
            NULL, NULL, JSPROP_ENUMERATE)) 
          } 
        else 
          {
          if (true == JS_DefineProperty(cx, o, "data", 
              OBJECT_TO_JSVAL(NULL), NULL, NULL, JSPROP_ENUMERATE))
            {
            QUEUE_EXCEPTION("Internal error!");
            goto err;
            }
          }
        }
      }
    }

No one seems to have noticed (or cared) on my examples and I got a 'best written' nomination on 'another forum' :)
FWIW
-BB-
 
BarrowBoy:

I'm old-school and use Allman/Whitesmiths [...]

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.

Reason: