# for in for cycle

Is possible make a cycle "for" inside another for cycle? I would like to improve this function:

void AggiornaPressione(){ origine=67; int c; for(c=7; c>=0; c--) { display.drawRect(origine, arrayColonne

``````, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+3, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+6, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+9, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+12, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+15, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+18, display.width()-124, display.height()-30, WHITE);
display.drawRect(origine, arrayColonne[c]+21, display.width()-124, display.height()-30, WHITE);
display.display();
origine=origine+8;
}

}
``````

Yeah, its a very common practice.

For examples, search for 'nested loops' or 'nested for'.

thanks

Try this

``````void AggiornaPressione()
{
origine=67;
int b,c;
for (c=7; c>=0; c--)
{
for (b=0; b<=21; b+=3)
display.drawRect(origine, arrayColonne[c]+b, display.width()-124, display.height()-30, WHITE);
display.display();
origine=origine+8;
}

}
``````

First, welcome to the Forum. To get the most from this Forum, please read the second two posts by Nick Gammon at the top of the Forum. It gives you guidelines for posting here, especially the use of code tags when you present source code listings.

Second, with the cursor in the source code window of the IDE, press Ctrl-T to reformat your code to a common C style.

Third, try using symbolic constants in your code, as shown here:

``````#define LEFTMARGIN    124          // Get rid of magic numbers...
#define TOPMARGIN      30
#define ORIGIN         67
#define ARRAYINDEX      7
#define BUMP            3
#define ENDBUMP        22
#define ORIGINBUMP      8

void AggiornaPressione() {
int origine = ORIGIN;
int c;
int i;

for (c = ARRAYINDEX; c >= 0; c--) {
for (i = 0; i < ENDBUMP; i += BUMP) {
display.drawRect(origine, arrayColonne[c] + i,
display.width() - LEFTMARGIN,
display.height() - TOPMARGIN,
WHITE);
}
origine += ORIGINBUMP;
}

}
``````

Symbolic constants serve two purposes: 1) they make your code easier to read by getting rid of “magic numbers”, and 2) they make it easier to change a constant that’s used in your program. For example, you may use the number 30 for the top display margin at dozens of places in your code. To change it to some new number, you must search-and-replace dozens of times. With a symbolic constant, you change the #define, recompile the program, and you’re done. Much less chance of inadvertently changing other constants incorrectly in the code.

You can also use something like:

const int TOPMARGIN = 30;

instead of a #define if you wish.

EDIT: You can get rid of a few more constants above:

``````    int b,c;
for (c=7; c>=0; c--)
{
for (b=0; b<=21; b+=3)
``````

Is better done as:

``````    for (int c=7; c>=0; c--)
{
for (int b=0; b<=21; b+=3)
``````

Then the relevant declarations of loop variables are in the obvious place, not floating about somewhere
further up. This makes things like cutting/pasting and re-arranging code less error-prone. Also any
declaration without an initializer is a potential bug - try not to.

Even more compact, using comma expressions:

``````void AggiornaPressione() {
for (int c=7, origine=67; c>=0; c--, origine+=8;)  {
for (int b=0; b<=21; b+=3)
display.drawRect(origine, arrayColonne[c]+b, display.width()-124, display.height()-30, WHITE);
display.display();
}
}
``````