Strange use of local variables

I am studying display programming. I use UTFT library. There in UTFT_Bitmap example I found this:

void loop()
{
  myGLCD.fillScr(255, 255, 255);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print(" *** A 10 by 7 grid of a 32x32 icon *** ", CENTER, 228);
  for (int x=0; x<10; x++)
    for (int y=0; y<7; y++)
      myGLCD.drawBitmap (x*32, y*32, 32, 32, info);

  delay(5000);
  
  myGLCD.fillScr(255, 255, 255);
  myGLCD.setColor(255, 255, 255);
  myGLCD.print("   Two different icons in scale 1 to 4  ", CENTER, 228);
  int x=0;
  for (int s=0; s<4; s++)
  {
    x+=(s*32);
    myGLCD.drawBitmap (x, 0, 32, 32, tux, s+1);
  }
  x=0;
  for (int s=4; s>0; s--)
  {
    myGLCD.drawBitmap (x, 224-(s*32), 32, 32, icon, s);
    x+=(s*32);
  }

  delay(5000);
}

What I cannot understand is this. In my opinion X and y from for statement are not valid in myGLCD.drawBitmap (x32, y32, 32, 32, info); function.

for (int x=0; x<10; x++)
    for (int y=0; y<7; y++)
      myGLCD.drawBitmap (x*32, y*32, 32, 32, info);

This sketch compiles well. X and y are probably defined elsewhere then?

LMI:

for (int x=0; x<10; x++)

for (int y=0; y<7; y++)
      myGLCD.drawBitmap (x32, y32, 32, 32, info);




This sketch compiles well. X and y are probably defined elsewhere then?

they are defined right there in the snippet!

int x and int y

Yes they are, but, what I know, they are not defined outside. (int x=0; x<10; x++ defined here) not defined here.

(int x=0; x<10; x++)
{
x is defined here
}

but not anymore here

They are what is known as "nested 'for' loops".

Benny_H88:
They are what is known as "nested 'for' loops".

Because of that late ;? That news to me.

I would have done it like this. - syntax errors

for (int x=0; x<10; x++)
{
    for (int y=0; y<7; y++)
{
      myGLCD.drawBitmap (x*32, y*32, 32, 32, info);
}
}

LMI:
Because of that late ;? That news to me.

No, because the “y” for loop is nested inside the “x” for loop.

Is your problem the lack of brackets? Brackets merely allow you to group a bunch of statements together. The for loop iterates over a statement which can be defined in two ways:

for (;;) //statement;

or

for (;;) { //statement}

both are equivalent, but the brackets allow you to put more than one statement. The code you question could have been written : (try it!)

for (int x = 0; x<10 x++)
{
    for (int y=0; y< 7; y++)
        {
             myGLCD.drawBitmap (x*32, y*32, 32, 32, info);
         }
}

I would always use parentheses with a for loop, even if they are not required. IN your code:

  for (int x=0;         //  <---- x comes to life here...
    x<10; x++) {
    for (int y=0; y<7; y++) {
      myGLCD.drawBitmap (x*32, y*32, 32, 32, info);   // x is very much alive here...
    }                                     // close of y loop
  }                     // <-----  x dies here, at the close of x loop

The life and visibility is set by the scope rules in C.

That should work, what was the error?

econjack:
Do you mean brackets? parens are required for the control statements in a for loop.

@KeithRB: yep.

I think I understand now. But I would definitely write it on the same long line. This is confusing.

for (int x=0; x<10; x++)
    for (int y=0; y<7; y++)
      myGLCD.drawBitmap (x*32, y*32, 32, 32, info);

LMI:
I think I understand now. But I would definitely write it on the same long line. This is confusing.

for (int x=0; x<10; x++)

for (int y=0; y<7; y++)
      myGLCD.drawBitmap (x32, y32, 32, 32, info);

I would still write it bracketed; I feel it is much easier to ‘notice’ the nested for loop so coming back to that block, one knows right away about what exactly to expect (the compiler on the other hand is a bit smarter than me).

My C++ editor doesn’t charge by the bracket… individual results may vary. :wink:

I agree.
Either in one long line or use (free) brackets.