On while statement a warning is given .. possible use of uninitialized variable 'i'

 

Hi, I am trying to put fractals on my chart with this code but on while(i>=2) statement, it says   --> possible use of uninitialized variable 'i'  

why is 'i' not getting initialized when its getting initialized in if statement..

..regards.

int start()

  {
   int    i;
   bool   bFound;
   double dCurrent;
   int nCountedBars=IndicatorCounted();

//---- last counted bar will be recounted    
   if(nCountedBars<=2)
      i=Bars-nCountedBars-3;
      
   if(nCountedBars>2)
     {
      nCountedBars--;
      i=Bars-nCountedBars-1;
     }
//----Up and Down Fractals
   while(i>=2)   << it gives warning here (possible use of uninitialized variable 'i')

  .................

  .................


 
Please edit your post and
use the code button (Alt+S) when pasting code

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
Ahmed Hussain: why is 'i' not getting initialized when its getting initialized in if statement..
   int    i;
   if(nCountedBars<=2) …      
   if(nCountedBars>2){…}
   while(i>=2)   << it gives warning here (possible use of uninitialized variable 'i')

If the first if is false and the second is also false, i is uninitialized. You are smarter than the compiler.

 
Ahmed Hussain:

Hi, I am trying to put fractals on my chart with this code but on while(i>=2) statement, it says   --> possible use of uninitialized variable 'i'  

why is 'i' not getting initialized when its getting initialized in if statement..

..regards.


Simply make the 

int i

to be a global variable.

 
Isaac Kimani:

Simply make the 

int i

to be a global variable.

Why?

Apart from anything else it is bad practice to declare variables globalscope when there is no need to.

 
i think 
int i = 0;
will work but there is another issue. I am not able to display fractal on the chart.
 
Ahmed Hussain:

Did you see my earlier post?

Keith Watford:
Please edit your post and
use the code button (Alt+S) when pasting code

Please do it.

 

Actually

int i = 0;

will fix the warning but again as William said, when both ifs are false you will not enter in the while and your fractal will not be displayed.

You have to change your logic.

 
Georgi Gaydarov:

Actually

will fix the warning but again as William said, when both ifs are false you will not enter in the while and your fractal will not be displayed.

You have to change your logic.

 Be it any value it will be less than or equal to 2 or greater than 2. Out of three any one will be correct. So I think logic is alright here. What say?

Reason: