Why mql4 doesnt see variables inside a if-else

 

I want to define variables depending on a condition so I write


if (CONDITION==0){
        double vol0 = AAAAAAA
        , vol1 = BBBBBBBBB
        , vol2 = CCCCCCCCC
        ;
        }
        else{
        double vol0 = DDDDDDDDD
        , vol1 = EEEEEEEEEE
        , vol2 = FFFFFFFFFFF
        ;
        }

double my_double = (vol0+vol1);

But mql4 says vol0 and vol1 are not defined when it tries to calculate my_double, whereas it is defined in all cases of the boolean CONDITION.

 

Hi,

when you declare a variable inside a {} block, it can only be used inside the block.
This is a very important fundamental of the MQL language - you should read the
documentation about variables and scope.
So you have to declare vol0 and vol1 before the if-statement.

Best regards

 

Please stop leaving your forum threads hanging! In all the threads you have started you have never come back to confirm if the answers given were satisfactory and resolved your issue or if you understood them. In all the cases you just left them hanging without a final response.

If you continue with this pattern of behavior, there will be a tendency to start ignoring your requests for help.

 

Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

Reason: