Posted By:
30 Jul, 2014 9:54 pm PDT

Switch operator in MetaTrader

SWITCH OPERATOR

Some programs imply branching of their algorithm into several variations. In such cases, it is very convenient to use the operator 'switch', especially if there are tens or hundreds of variations, whereas the 'if' code becomes bloated if you use many nested operators.

Format of Switch Operator

The operator 'switch' consists of the header and the executable body. The header contains the name of the operator and an Expression enclosed in parentheses. The operator body contains one or several variations 'case' and one variation 'default'.

Each 'case' variation consists of the key word 'case', a Constant, ":" (colon), and operators. The amount of variations 'case' is not limited.

The variation 'default' consists of the key word 'default', ":" (colon), and operators. The variation 'default' is specified in the body of the operator 'switch' as the last, but it can also be located anywhere within the operator body or even be absent.

switch ( Expression )// Operator header
{// Opening brace
case Constant: Operators// One of the 'case' variations
case Constant: Operators// One of the 'case' variations
...
[default: Operators]// Variation without any parameter
}

 

The values of Expression and of Parameters can only be the values of int type. The Expression can be a constant, a variable, a function call, or an expression. Each variation 'case' can be marked by an integer constant, a character constant, or a constant expression. A constant expression cannot include variables or function calls.

Flowchart of switch operator

Attachment:None


Example of switch operator:

//+------------------------------------------------------------------+
//| properties.mq4 |
//| Copyright 2014, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---

{
double Level=0.3200;
int Delta=NormalizeDouble((Bid-Level)/Point,0);
Alert("Delta "+Delta);

if (Delta<=0)
{
Alert("Negative value");
return 0;
}
//--------------------------------------------------------------------
switch(Delta)
{
case 1 : Alert("Value of delta is 1"); break;
case 2 : Alert("Value of delta is 2"); break;
case 3 : Alert("Value of delta is 3"); break;
case 4 : Alert("Value of delta is 4"); break;
case 5 : Alert("Value of delta is 5"); break;
case 6 : Alert("Value of delta is 6"); break;
case 7 : Alert("Value of delta is 7"); break;
case 8 : Alert("Value of delta is 8");break;
case 9 : Alert("Value of delta is 9");break;
case 10: Alert("Value of delta is 10");break;
default: Alert("Value of delta is more than 10");
}
//--------------------------------------------------------------------
return 0;
}

}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
Print("Deinit function");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
Print("ONTick function");
}
//+------------------------------------------------------------------+
 
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud