Please, why is that?

 

Please, why?


void OnStart()

  { 

   uchar  ch; 

  

   for(char ch=1;ch<=9;ch++) 

     { 

      u_ch=ch; 

      Print("ch = ",ch," u_ch = ",u_ch); 

      if(ch==9) break; 

     } 

  }
Files:
 
helloea:

Please, why?


Try this

void OnStart()

  { 

   uchar  u_ch; 

  

   for(char ch=1;ch<=9;ch++) 

     { 

      u_ch=ch; 

      Print("ch = ",ch," u_ch = ",u_ch); 

      if(ch==9) break; 

     } 

  }
 
Nikolaos Pantzos:

Try this

Thank you !!

I want to know why "uchar u_ch;"And"for(char ch=1;ch<=9;ch++)  "I can't use ch for both of them。

 
  1. helloea: I want to know why "uchar u_ch;"And"for(char ch=1;ch<=9;ch++) "I can't use “ch” for both of them。

    You can. What you shouldn't do is declare them twice. What you can't do is not declare one at all.

       uchar  ch;
       for(char ch=1;ch<=9;ch++){
          u_ch=ch; 

    Your original image declared a variable ch (unsigned character.) Then created a new variable in the for loop with the same name. This hide the outer ch (very bad practice.) If you wanted to use the original ch and not create a new one, you would just write for(    ch=1; …)

  2. No where in your code did you ever declare a variable u_ch, thus your "undeclared identifier."

  3. Please don't post an image of code or the editor — it is generally to small to be read.

    Only use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

 
William Roeder:
  1. You can. What you shouldn't do is declare them twice. What you can't do is not declare one at all.

    Your original image declared a variable ch (unsigned character.) Then created a new variable in the for loop with the same name. This hide the outer ch (very bad practice.) If you wanted to use the original ch and not create a new one, you would just write for(    ch=1; …)

  2. No where in your code did you ever declare a variable u_ch, thus your "undeclared identifier."

  3. Please don't post an image of code or the editor — it is generally to small to be read.

    Only use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor


Thank you, I seem to know the first and second, the third I will remember, thank you again!Hope we communicate more!

Reason: