If else variable

 

Hello mql guru's,

Is it possible to use an "if/then" condition for creating a variable? This is what I need:

double Var32 = if (Var17 < Var18) Var18 - Var17; else Var17 - Var18;


of course this won't compile, but how can I get the data I need into a variable form?

I'm obviously a newbie, and Thanks in advance.....
eb-

 
variables are created during compilation with a variable declaration, mql4 is a static language, all variables (their name and their type, not their content) must be declared before they are used:

https://docs.mql4.com/basis/variables

This has nothing to do with if/else which is there to control the program flow. You can do whatever you want with your variables in an if or else or elsewhere, as long as they have been properly declared before.
double Var32;

if (Var17 < Var18){
   Var32 = Var18 - Var17;
}else{
   Var32 = Var17 - Var18;
}


// of course the above is nonsense, although it compiles and works as expected.
// you would instead use the following to achieve the same goal:

double Var32;

var32 = MathAbs(Var17 - Var18);
If you are asking for the ternary operator, there is no such thing in mql4 and IMHO this is also not needed at all in an imperative language. It only serves the purpose of obfuscating otherwise perfectly readable code.
 
7bit wrote >>
variables are created during compilation with a variable declaration, mql4 is a static language, all variables (their name and their type, not their content) must be declared before they are used:

https://docs.mql4.com/basis/variables

This has nothing to do with if/else which is there to control the program flow. You can do whatever you want with your variables in an if or else or elsewhere, as long as they have been properly declared before.If you are asking for the ternary operator, there is no such thing in mql4 and IMHO this is also not needed at all in an imperative language. It only serves the purpose of obfuscating otherwise perfectly readable code.


Thanks 7bit. That worked out great!
Reason: