gharryh
November 18, 2019, 2:37pm
#1
I have this simple piece of code
void loop() {
int randNumber = random(1, 16384);
TFTscreen.drawRect(0, 0, 128, 128, ST7735_YELLOW);
TFTscreen.setTextColor(ST7735_RED, ST7735_BLACK);
int row = 0;
for (int c = 13; c < 14; c++) {
Serial.println(row);// debug
TFTscreen.setCursor(128, row);
TFTscreen.println(random(1, 16384));
TFTscreen.setCursor(128, row + 10);
TFTscreen.println(random(1, 16384));
row = row + 10;
Serial.println(row);//debug
}
}
But the only value the int row has are 0 and 10 but not higher. as it should.
Suggestions
vaj4088
November 18, 2019, 2:57pm
#2
How many times will this for-loop execute ?
for (int c = 13; c < 14; c++)
Your statement:
for (int c = 13; c < 14; c++) {*
says that there will be one pass through the loop. So the first pass prints row = 0, the second print statement comes after row is increased by 10. Then the 3rd expression in the for loop above increment c , so it is now 14. Expression 2 now says we’re done with the loop.
Basically, your for loop is saying
If c (which is 13) is less than 14
Do this stuff
Then add one to c (making it 14)
Then it restarts from the beginning of the for loop.
c now eguals 14. Which is not less than 14. So the loop exits having done what it was instructed.
So, in your case, you only get one trip through the for loop.