Download MetaTrader 5

Question about combining IF

To add comments, please log in or register
Let your idle PCs do something useful. Join MQL5 Cloud Network!
Harry
267
Harry 2013.12.03 05:27 

Hi Everyone,

I have a list of condition say (A, B, C, D, E, F).

I want to write a code that say: If 2 or more condition in the list are met, then return value=1.

If I try to do the regular IF, it will be very long since there are many combination.

I wonder if there is any other better approach.

I appreciate your help.

SCFX

onur
5
onur 2013.12.03 08:21  
scfx:

Hi Everyone,

I have a list of condition say (A, B, C, D, E, F).

I want to write a code that say: If 2 or more condition in the list are met, then return value=1.

If I try to do the regular IF, it will be very long since there are many combination.

I wonder if there is any other better approach.

I appreciate your help.

SCFX


if i correctly understand, you can define a static or global variable and in which statements are true,variable++ and in the last if variable >=2 return(1), and after set variable to zero,it goes so on.
GreenMoney
379
GreenMoney 2013.12.03 13:56  
scfx:

I have a list of condition say (A, B, C, D, E, F).

I want to write a code that say: If 2 or more condition in the list are met, then return value=1.

If I try to do the regular IF, it will be very long since there are many combination.

I wonder if there is any other better approach.

Instead of trying to code one very long IF statement, why not use smaller and simpler ones. Consider the following code (assumes variables A, B, C, D, E, and F are bools):

int value = 0;
if (A && (B || C || D || E || F))
   value = 1;
else if (B && (A || C || D || E || F))
   value = 1;
else if (C && (A || B || D || E || F))
   value = 1;
else if (D && (A || B || C || E || F))
   value = 1;
else if (E && (A || B || C || D || F))
   value = 1;
else if (F && (A || B || C || D || E))
   value = 1;

If any two members of the list (A, B, C, D, E, or F) are true, the variable "value" is set to 1.

whroeder1
14480
whroeder1 2013.12.03 14:08  
scfx: I want to write a code that say: If 2 or more condition in the list are met, then return value=1.
  1. You don't want to return a value of one. You either want to return a bool (don't use 0 or 1 when you mean true or false,) or you want to just return the actual count and let the caller decide
  2. So do exactly that, count how many are true.
    bool fcn(){
      bool isA = ...,
           :
           isF = ...;
      int nTrue = 0;
      if(isA) nTrue++;
      :
      if(isF) nTrue++;
      return(nTrue >= 2);
    }
Harry
267
Harry 2013.12.03 17:04  
WHRoeder:
  1. You don't want to return a value of one. You either want to return a bool (don't use 0 or 1 when you mean true or false,) or you want to just return the actual count and let the caller decide
  2. So do exactly that, count how many are true.


Thank you all for your help.

I will make the counting as you all suggested instead of falling into individual combination. It is way simpler than I first thought.

Have a nice day,

SCFX

To add comments, please log in or register