Flow control using char variable - page 2

 
JimSingadventure #:

Thanks for quick reply Paul.

i do not understand your comments though because i have no background in IT and just self-taught in programming by reading and learning from guys like you. What do you mean by "i did not initialised alertswitch in FnAdd?" i thought by adding char in front of alertswitch at the top of the FnAdd, it is already initialized and can be 'seen' by all if statements since they are all within the FnAdd bracket? what else needs to be done? 

appreciate if you can go down to my level to explain...

separately, 

1. i added [#property strict] at the top of my code (both this sample above and my full working code)  and they both compiled the same withouth errors. 

2. there does not seem to be any documentation about #property strict in MQL5 : https://www.mql5.com/en/docs/basis/preprosessor/compilation

regarding property strict, just always use it.  I compiled in MT4 and it does warn of possible non-assignment, I just tried MT5 and for some reason it does not - either a bug in the MT5 compiler or just a difference.

regarding variables:

char MyVariable;        // this declares a char variable but gives it no value.

char MyVariable = 50;   // this declares and then assigns 50 as a value to MyVariable

MyVariable = 50;        // assigns a value to an already declared variable

where you declare them is important. 

If you declare them in the global scope of the program then they are available everywhere (can be seen and used).

If you declare a variable within a function it is only available inside the function.

You have TWO separate variables called alertswitch one in OnTick and the other in FnAdd - they are different variables and you should avoid using the same name to avoid confusion.

In FnAdd  you declare the variable but never assign a value to it before you start checking it with the IF statements, therefore you are checking an unknown value so how can you predict the result?

 
Paul Anscombe #:

regarding property strict, just always use it.  I compiled in MT4 and it does warn of possible non-assignment, I just tried MT5 and for some reason it does not - either a bug in the MT5 compiler or just a difference.

regarding variables:

where you declare them is important. 

If you declare them in the global scope of the program then they are available everywhere (can be seen and used).

If you declare a variable within a function it is only available inside the function.

You have TWO separate variables called alertswitch one in OnTick and the other in FnAdd - they are different variables and you should avoid using the same name to avoid confusion.

In FnAdd  you declare the variable but never assign a value to it before you start checking it with the IF statements, therefore you are checking an unknown value so how can you predict the result?

Thanks Paul for explanation and patience!

so i was assuming that by simply 

char alertswitch;

that i am already implicitly assigning a NULL or zero value to alertswitch by default, when in fact i should explicitly type 

char alertswitch=0;

to be correct and to remove the warnings!

==


but going back to the original question, how does this affect the [if-only] statement and not affect the [else if]? ...i'm still fuzzy about which one is better or the correct statement to use..