# While loop inside for loop

Hello everyone, I have been writing this program, is an interface for an LCD, the only problem I have is in this part:

``````void labelsMenu() {
for(int x; x < 3; x++){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Label Obj. ");
lcd.print(x+1);
lcd.print(":");
position = 3;
lcd.setCursor(position,2);
char l = 'A';
lcd.print(l);
for (int i; i < 15; i++) OBJ[x][i] = 0;
OBJ[x][0] = l;
for (int i; i < 15; i++){
do {
delay(250);
Serial.print("HELLO");
delay(250);
l--;
if (position < 3) position = 3;
}
delay(250);
l++;
if (position > 15) position = 15;
}
if (l > ('Z' + 15)) l = 'A';
else if (l < ('A' - 1)) l = ('Z' + 15);
if (l == ('Z' + 1)) OBJ[x][i] = ' ';
else if (l == ('Z' + 2)) OBJ[x][i] = ',';
else if (l == ('Z' + 3)) OBJ[x][i] = '.';
else if (l == ('Z' + 4)) OBJ[x][i] = '-';
else if (l == ('Z' + 5)) OBJ[x][i] = '_';
else if (l == ('Z' + 6)) OBJ[x][i] = '0';
else if (l == ('Z' + 7)) OBJ[x][i] = '1';
else if (l == ('Z' + 8)) OBJ[x][i] = '2';
else if (l == ('Z' + 9)) OBJ[x][i] = '3';
else if (l == ('Z' + 10)) OBJ[x][i] = '4';
else if (l == ('Z' + 11)) OBJ[x][i] = '5';
else if (l == ('Z' + 12)) OBJ[x][i] = '6';
else if (l == ('Z' + 13)) OBJ[x][i] = '7';
else if (l == ('Z' + 14)) OBJ[x][i] = '8';
else if (l == ('Z' + 15)) OBJ[x][i] = '9';
if (l <= 'Z' && l >= 'A') OBJ[x][i] = l;
lcd.setCursor(position,2);
if(l != ('A' - 1)) lcd.print(OBJ[x][i]);
if (l < 'A'){
lcd.setCursor(position,2);
lcd.write(1);
}
}
position++;
if (l < 'A'){
OBJ[x][i] = ':';
OBJ[x][i+1] = ' ';
i = 15;
break;
}
l = 'A';
OBJ[x][i+1] = l;
lcd.setCursor(position, 2);
lcd.print(l);
}
for (int i = 0; i < 15; i++)
EEPROM.write(4 + i, OBJ[x][i]);
}
}
``````

At first I’ve tested it only for OBJ 0, and it works fine, as expected, but when I added the for loop to do that rutine 3 times for the 3 Objects I have, it skips the while loop. I don’t think it is a stack overflow, but it may be, so I want to ask you all if you know why this is happening or how to handle with this.

`  for (int x; x < 3; x++)`What is the initial value of x ?

Well, x starts in 0 if it is not specified, the same happens with the for loop with i.

x starts in 0 if it is not specified

Have another guess.

I have just tried initializing X with 0 in the for loop, also with i in its respective loop, but the same happens, the while loop is being skipped.

So, post the changed code.

Here it is

``````void labelsMenu() {
for(int x = 0; x < 3; x++){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Etiqueta Obj. ");
lcd.print(x+1);
lcd.print(":");
position = 3;
lcd.setCursor(position,2);
char l = 'A';
lcd.print(l);
for (int i = 0; i < 15; i++) OBJ[x][i] = 0;
OBJ[x][0] = l;
for (int i = 0; i < 15; i++){
do {
delay(250);
Serial.print("Entro en bucle While");
delay(250);
l--;
if (position < 3) position = 3;
}
delay(250);
l++;
if (position > 15) position = 15;
}
if (l > ('Z' + 15)) l = 'A';
else if (l < ('A' - 1)) l = ('Z' + 15);
if (l == ('Z' + 1)) OBJ[x][i] = ' ';
else if (l == ('Z' + 2)) OBJ[x][i] = ',';
else if (l == ('Z' + 3)) OBJ[x][i] = '.';
else if (l == ('Z' + 4)) OBJ[x][i] = '-';
else if (l == ('Z' + 5)) OBJ[x][i] = '_';
else if (l == ('Z' + 6)) OBJ[x][i] = '0';
else if (l == ('Z' + 7)) OBJ[x][i] = '1';
else if (l == ('Z' + 8)) OBJ[x][i] = '2';
else if (l == ('Z' + 9)) OBJ[x][i] = '3';
else if (l == ('Z' + 10)) OBJ[x][i] = '4';
else if (l == ('Z' + 11)) OBJ[x][i] = '5';
else if (l == ('Z' + 12)) OBJ[x][i] = '6';
else if (l == ('Z' + 13)) OBJ[x][i] = '7';
else if (l == ('Z' + 14)) OBJ[x][i] = '8';
else if (l == ('Z' + 15)) OBJ[x][i] = '9';
if (l <= 'Z' && l >= 'A') OBJ[x][i] = l;
lcd.setCursor(position,2);
if(l != ('A' - 1)) lcd.print(OBJ[x][i]);
if (l < 'A'){
lcd.setCursor(position,2);
lcd.write(1);
}
}
position++;
if (l < 'A'){
OBJ[x][i] = ':';
OBJ[x][i+1] = ' ';
i = 15;
break;
}
l = 'A';
OBJ[x][i+1] = l;
lcd.setCursor(position, 2);
lcd.print(l);
}
for (int i = 0; i < sizeof(OBJ[x])-1; i++)
EEPROM.write(4 + i, OBJ[x][i]);
}
}
``````

Edit: I’ve also try printing the value of x and i in the serial monitor, and they have the expected values

How is the selection_button input pin wired ? What pinMode() was used for it ?

I'm not sure what you mean by 'skips the while loop': do you mean it fails to run all the code between 'do' and 'while'? If so then it couldn't print the values of x and i in the serial output, which you say you tried.

Or, do you mean that it jumps out of the 'while' loop too soon, in which case 'digitalRead' isn't returning what you expect. But then x wouldn't increment properly.

It might help if you posted the whole code rather than one function.

Also can I suggest never using the letter 'l' as a variable, it looks too much like a constant'1' and is confusing for the reader!