What is Truth? The if Statement

What is truth?

Powered By S3 Media Maestro

If you are a bible reader you may recognize that as the famous rhetorical question which Pontius Pilate asked Jesus . If you are a country music fan, you may remember a country music song by Johnny Cash with the same title “What Is Truth”.   And then of course if you are a movie watcher you may remember Jack Nicholson’s famous rant where he tells us “We can’t handle the truth!”. Everyone is interested in finding out what is true. Sometimes we can get different versions of the “truth” from witnesses to an event. There is more than one version of the “truth”.

When it comes to coding in Mql4 you may be surprised to find out how little it takes to make something appear as  true. When we want to know if a certain thing is true we usually say something like……

if( Price < MovingAverage ) { OpenSell(); }

If statements are all about finding out if something is true or not. This is really saying
"If it is true (that the price is less than the moving average) { Open a sell; }
If it is true then we move on to do what’s within the brackets.

Now you may think that the ‘true or false’ question can only be asked of a boolean type variables that are either set to true or false… or some comparison which returns a true or false answer.  For instance, Let’s say if we have an external setting in our code called “Hedging_Is_Allowed”
bool Hedging_Is_Allowed = true;
We could write…..
if(Hedging_Is_Allowed == true){Enterbuy();}

What I want you to learn from this lesson is what it really means when something is true.  What is it really saying about that thing? And what is the thing that’s true? Because, in mql4 coding just like in the real world there are multiple things that mean “true”.

The reason this if statement will make it enter a buy is not because hedging is allowed is set to true. Rather, it’s because the comparison ended up being true. It’s the logical comparison that has to be true. It is the ‘thing that’s true’.  If we had said…
if(Hedging_Is_Allowed == false)
 and we still have Hedging_Is_Allowed set to true, the buying would not have happened because the comparison..  turned out to be false. But what does ‘false’ really mean?

As stated on this page The boolean settings of  TRUE and FALSE actually have the numeric representation of 1 for true and 0 for false. That is to say that whenever we have a boolean variable set to false it is set to 0. Whenever we set something to true, it is set to a 1.

So whenever what is contained inside the if’s parentheses is either a false logical statement or is actually the number 0, is the only time the if’s operator won’t happen. Here is what a lot of people miss. The ONLY thing that equals false is zero. It has to be zero to be false.

HOWEVER, true does NOT have to be 1.Yes it is true that TRUE is the numeric representation of  1,  but it  works because it is NOT zero... not because it is 1..  Note this statement on that same page

“It should be noted that in logical expressions you can use other integer or real types(types with a decimal point.(double, float)) or expressions of these types – the compiler will not generate any error. In this case, the zero value will be interpreted as false, and all other values – as true.”

 

So… What is truth?…. It’s a lot of things…. anything that is NOT zero. Not only does 1 mean true but also 17 or 365 or any number besides zero! Yes, when we set our Hedging to true we are essentially setting it to 1.  In the statement…
if(Hedging_Is_Allowed == true){Enterbuy();}
Really the computer sees it as …
if ( 1 == 1)
Keep in mind this is a logical expression comparing these two numbers. The comparison is true and therefore what is really the result of the logical comparison is a 1. Which is not zero…. which is what really matters, and what makes it go ahead and enter a buy order or whatever.

In that case the comparison equaled something that was not zero. Due to the fact that a boolean type variable is seen by the computer as either being a 0 or a 1, all we really have to put in the parentheses is that variable. If it happens to be set to true(1) at the moment or false(0) that’s all we need to know…
if(Hedging_Is_Allowed){EnterBuy();}
When Hedging_Is_Allowed is set to true.
Is simply seen as..
if(1){EnterBuy();}
When Hedging_Is_Allowed is set to false.
if(0){EnterBuy();

  • To Recap…
  • an if statement will run it’s operation that comes after it only when it is true.
  • This does not necessarily mean 1.. it really means NOT zero.
  • 47625 in an if statement would be true
  • only one thing means false… a zero.

Now here is the next question… since our if statement is always doing something if a true condition exists (not zero). What do we use for a conditional statement if we want it to only do something when the if statement is false?

Let’s say we only want to enter a trade when we are sure that there are not trades already open. When it’s false that there are trades open.. Or when it’s false that it is friday afternoon. How do we make sure it is NOT some condition instead of it IS some condition?…..

NEXT PAGE