⚡ Decision Making Statements IF-Else in MQL

 

Decision Making Statements IF-Else Switch Statements in MQL.png

Decision Making Statements IF-Else in MQL ⚡

What is Decision Making?

Yep, You have daily life experience of Decision Making frequently. How?

Let's take an example,

Suppose, You are a Teacher in #School and you are teaching. One Student knock on the door and said that "May I come in Sir!".

Decision Making Statement Real life example


🤠 You have a fixed Mindset that you do not allow students without having Books. Then this is the condition of your decision-making. Because Entering of Student is totally dependent upon the condition of "Having Books". If the student has books then the condition will be "True" and He gets entered into the classroom. otherwise, the condition will be "False" and He does not get entry into the classroom.

Hence, 

Decision Making is the processing mechanism for full-filling the desired condition which is already mentioned in the System.

Decision Making in MQL can be implemented using If-Else Statements like C and C++. It is very easy to understand. 

Types of Decision Making Statements in MQL

Decision Making Statements can be implemented by using 4 types of Statements which are as follows:-
  1. Simple If Statement
  2. If-Else Combination Statement
  3. Else-If ladder Statement
  4. Switch Statement
I discuss one by one with pre-defined syntax and examples with explanations.

Simple IF Statement in MQL

"Simple If Statement" is used for implementation of easiest logic in the Program of MQL Forex Trade programing language.

Syntax :  

if(expresssion)
{
    // write your logic here
}

If the value of expression gives value as "TRUE" then only code inside the Braces executes. If the value of the expression given is "False" then if statement will not execute.
Here, Open Brace Symbol " { " is used for starting the if-block or if-statement and Close Brace Symbol " } " is used for ending the if-block or if-statement.

A portion of Code which is inside the Open "{" and Close "}" brace symbol is called "Block". This Block is the Body of IF-STATEMENT.

Before we take an example for learning the Simple If Statement, you should consider the following Rules.
  • You can use Multiple Simple If-Statements in Single MQL Program Code.
  • You can use Nested any other decision-making statement inside simple If Statement, here Nested means one inside another. Example - You can use Simple If Statement inside another "If Statement" also. 
  • If you are writing only one line of code after "if(expression)" then you can avoid the use of open and close brace symbols. The system will automatically treat the first line of code as a part of the Simple If statement. 
Source Code Example - "Check Positive Number Program in MQL"
void OnStart() { // we are learning if-else statement int x = 10; if(x > 0) Alert("X is Positive Number"); if(x < 0) Alert("X is Negative Number"); } 

Output
Decision Making Statements IF-Else Positive Number Checking Program in MQL


Explanation of Output

In this example, inside the first, if statement condition passed as x < 0 and x has value as 10 which is given "TRUE".

because "10 > 0" is the true statement. here I have used only a single line of code after the if statement that's why I did not use Open and Close Braces for defining the Body of If Block and you get output from the first if statement as "X is Positive Number" using Alert Function. 

In the case of the second if statement, the condition inside the second if statement gets wrong or false that's why it did not execute.

If-Else Combination Statement in MQL

"If-Else Combination Statement" is used for implementation of logic of both conditions TRUE and FALSE in the Program of MQL Forex Trade programing language.

Syntax :  

if(expresssion)
{
    // write your logic for True Condition only
}
else 
{
    // write your logic for False Condition only
}

If the value of expression inside "if" gives value as "TRUE" then only code inside the "if" block executes and If the value of the expression is "FALSE" then code inside the "else" execute.

Here, Open Brace Symbol " { " is used for starting the "if" and "else "block, and Close Brace Symbol " } " is used for ending the "if" and "else" block.

Before we take an example for learning the If-Else combination Statement, you should also consider the following Rules.
  • You can use Multiple Simple If-Else Statements in Single MQL Program Code.
  • You can use Nested any other decision-making statement inside If-Else Statement, here Nested means one inside another. Example - You can use If-Else Statement inside another "If Statement", "If-Else", "Else-If ladder" also. 
  • If you are writing only one line of code after "if(expression)" or "else" then you can avoid the use of open and close brace symbols. The system will automatically treat the first line of code as a part of the If or else statement. 
  • You can not write the "else" statements alone. There must be a matching "If" Statement with an "else" statement.

Source Code Example - "Check Even and Odd Number Program in MQL"

void OnStart() { // we are learning if-else statement int x = 19; if(x %2 == 0) Alert("X is Even Number"); else Alert("X is Odd Number"); } 

Output 
Decision Making Statements IF-Else Even Odd Number finding Program in MQL


Else-If ladder Statement in MQL

"Else-If Ladder Statement" is used for implementation of logic of both conditions TRUE and FALSE with a deep hierarchy of conditions in the Program of MQL Forex Trade programing language.

Syntax :  

if(expresssion)
{
    // write your logic 
}
else if (expression) 
{
    // write your logic 
}
else
{
    // write logic for False Condtion Case.
}

If the value of expression inside first "if" gives value as "TRUE" then only code inside the "if" block executes and If the value of the expression is "FALSE" then program control shifts to the immediate next "else-if" decision-making statement "else-if". Program Control or Compiler checks this "else-if" expression. if this is found as "TRUE", then the code inside this block executes. If found as "False" then compile moves to the next "else-if" for checking.

This conditional checking process continues till the reach of the last else statement.

Before we take an example for learning the If-Else combination Statement, you should also consider the following Rules.
  • If any block of the "else-if " statement matches and gets "TRUE" then the other block did not execute. Execution of the "Else-If" ladder only continues till the program does not find any match. "Else-If" block statement execution ends when the program finds any True expression condition.
  • You can use Nested any other decision-making statement inside the "else-if" ladder statement, here Nested means one inside another. Example - You can use simple-if, If-Else Statement inside "Else-If ladder" and vise-versa. 
Source Code Example - "Program in MQL for calculation of Grade as per the Marks "
void OnStart() { // we are learning else-if statement int marks = 76 ; if(marks > 80) { Alert("Grade : A+"); } else if(marks > 60) { Alert("Grade : A"); } else if(marks > 40) { Alert("Grade : B"); } else { Alert("Grade : C"); } }

Output
Decision Making Statements else if ladder statement in MQL

Explanation of Output
In this example, 
I try to explain the basic usage of the else-if ladder by the implementation of the "calculation of grading using marks".
here, marks have a value of 66. first program control moves to the first if statement marks > 80, which is false, in this case.
Then program control moves to the next "else-if" which is marks > 60, this matches "TRUE" and Program execution exits from the "Else-If" ladder statement and you get the output of "Grade A".

Switch Statement in MQL

Switch Statement is used for providing decision-making on a large set of data as compare to if-statements. You can create any number of cases and pass a variable OR expression for matching with these user-defined cases. 

Syntax
switch(expression) { case constant: // your logic here break; case constant: // your logic here break; . . . default: operators }

  • The "expression" or variable passed inside the switch statement is checked with each "constant" defined in each case. if it matches with the value of the constant then the logic or code written in the block of the matched case is executed.
  • You must use the "break" keyword for exit from the current case. so that an un-matched case will not execute.
  • There will some applications where we use a single "break" for multiple cases.
  • default case will be executed if there is not matched case found in the switch statement.
Note:- In MQL4, the Expression of the switch operator must be of integer type.

let's discuss the real-life implementation of the switch case statement in terms of Forex Trading Automation.

Source Code Example - "MQL Program for Trading only on Working Days."
void DayOfWeekAlert() { int dayOfWeek = DayOfWeek(); switch (dayOfWeek) { case 1 : Alert("We are Monday. Let's try to enter new trades"); break; case 2 : Alert("We are tuesday. Let's try to enter new trades or close existing trades"); break; case 3 : Alert("We are wednesday. Let's try to enter new trades or close existing trades"); break; case 4 : Alert("We are thursday. Let's try to enter new trades or close existing trades"); break; case 5 : Alert("We are friday. Close existing trades"); break; case 6 : Alert("It's the weekend. No Trading."); break; case 0 : Alert("It's the weekend. No Trading."); break; default : Alert("Error. No such day in the week."); } }

Output
Decision Making Statements Switch Statement Example Trade only on Working Days MQL

Explanation of Output
In, this example, you can see that we have used a variable dayOfWeek and initialize the value from the pre-defined function DayOfWeek. This function gives us the value of the current day as per the (0-Sunday,1,2,3,4,5,6) pre-defined set of days.
When I run this code, the Last Known server time is on Friday. that's why this function DayOfWeek gives the value "5". and this value is further passed in the switch statement. "5" is matched with "case 5" and you see the output as "We are Friday. Close existing trades."

Summary

Decision Making the most important part of coding the software, web, or a Trading Robot. You can take the decision on the basis of well-defined conditions using decision-making. In MQL, You can implement decision-making using the Simple if statement, If-Else combine statement, Else-If ladder, and switch statement.