Why does this keep looping and skipping over the last 6 lines?

I wanted to see the contents of the EEPROM on a 5v Pro Mini, so I made a loop which prints to the serial monitor and put it inside an IF block. I thought it would only run once because the IF would only evaluate as "true" on the first pass. But it just keeps repeating the loop and doesn't even seem to complete the whole loop before returning to the top because of the 6 lines at the end with Serial.print which never show up in the serial monitor. I am totally baffled, I can't make any sense out of this execution.

#include <EEPROM.h>

int16_t address = -1;
float data;
uint8_t footprint = sizeof(float);
uint8_t i;

void setup() {
  Serial.begin(57600);
  Serial.print("The size of a float is ");
  Serial.println(footprint);
  delay (2222);
}

void loop()  {
  if (address < 0)  {
    for (i = 0; i < 256; i++)  {
      Serial.print("random FLOAT = ");
      address = i * footprint;
      EEPROM.get(address, data);
      Serial.print(data);
      Serial.print("    ");
      if (i % 4 == 3)  {
        Serial.print("     index = ");
        Serial.println(i);
      }
    }
    Serial.print("***********************   address = ");               // THESE 
    Serial.print(address);                                              // LINES
    Serial.println("   *************************");                     // NEVER
  }                                                                     // SEEN
  Serial.print("***********************   address = ");                 // IN
  Serial.print(address);                                                // SERIAL
  Serial.println("   *************************");                       // MONITOR
}

COPY AND PASTE from Serial Monitor.txt (7.27 KB)

uint8_t i;
    for (i = 0; i < 256; i++)  {

Sorry, I don't understand what you were trying to tell me. I tried

    for (uint8_t i = 0; i < 256; i++)  {

but there was no change.

I can't understand how the for loop seems to finish and then start over from the top, but the code right before it and right after it does not execute.

If the IDE warnings (File menu, Preferences) were turned on you would see this warning:

C: ... arduino_modified_sketch_568356sketch_dec19a.ino:21:21: warning: comparison is always true due to limited range of data type [-Wtype-limits]

       for (i = 0; i < 256; i++)

                   ~~^~~~~

What that is trying to tell you is that your variable (i) is 8 bits so can hold a value of 0 to 255. The limit in the for loop is 256. The variable (i, 0 to 255) can never be > 256 so the for will run forever. For uint8_t or byte, 255 + 1 = 0

Thanks so much for the explanation, it now seems painfully obvious! I have to admit that my compiler warnings was set to "all", I just wasn't paying attention. Even if I did, chances are I wouldn't have known what to make of it so I appreciate the help. This also explains why putting the loop in setup() didn't help. I just used uint16_t and deleted the last 3 lines and everything works fine. Thanks again for the assistance!

(I am a novice in Python as well, so it's good to know how for loops behave differently between the two languages. In Python**,** after the loop ends the variable is the same as it was inside the loop. I now see that with Arduino, the variable wants to be greater than it was inside the loop by the increment amount. I can see how not knowing this would bite me in the future.)

I now see that with Arduino, the variable wants to be greater than it was inside the loop by the increment amount.

Bearing in mind that for loops are intended to execute a set of commands a fixed amount of times then you already know what the value of the loop variable will be at the end of the loop. It is also usually considered good practice to declare the loop variable in the for loop initialisation to limit its scope so its value is not available outside of the for loop an any case

...declare the loop variable in the for loop initialisation...

I've seen that before, but never understood/appreciated why that was done. That's why I tried switching to that approach in #2 (above). I always declared just once to save keystrokes, but now I see the benefit of spending the extra time typing. Thanks for taking the time to make this point!