Program finishes too fast sometimes to be correct

I show 3 slightly different versions of the same program below. Each program should theoretically take about 10 seconds to finish.

I basically just need to loop some functions over and over a specific number of times and for a specific duration for each part of the loop. Oh and just so you all understand why I did the nested loops when you view the code, I found that the arduino doesn't like it if I try to tell it to do more than 30,000 loops in one statement so I made two nested loops to enable me to go beyond that threshold. If I don't do that and tell it to do 100,000 loops in one statement, for instance, it will get stuck and just stay on forever. I'm not sure why that is or if it is or if it is at all related to the problem I'm presenting below...

Ok well now to the interesting part:

In the first program I have 1,000 loops to perform (10*100) and each loop contains two 5 ms delays. This should take 10 seconds to finish and it does exactly as expected.

//--- Digital Pins - use as many as u need 

int relayPin7 = 7;//these are the digital out pins on the arduino
int relayPin8 = 8;
int relayPin9 = 9;
int relayPin10 = 10;
int relayPin11 = 11;
int relayPin12 = 12;
int relayPin13 = 13;

void setup()
{   
	//--- Set up the  outputs   
   
        pinMode(relayPin7, OUTPUT);
        pinMode(relayPin8, OUTPUT);
        pinMode(relayPin9, OUTPUT);
        pinMode(relayPin10, OUTPUT);
        pinMode(relayPin11, OUTPUT);
        pinMode(relayPin12, OUTPUT);
        pinMode(relayPin13, OUTPUT);
	
	//--- open the serial port  - unneccessary, but lets you see output on computer screen
	Serial.begin(9600);
}

void loop()
{   
{
  for (int a=1; a <= 10 ; a++)//loops # times
{
  for (int i=1; i <= 100; i++)//loops # times
	{  	     
        digitalWrite(relayPin7, HIGH); // Make all pins either High or Low (SET operation)
	digitalWrite(relayPin8, HIGH); 
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, LOW);
        digitalWrite(relayPin11, LOW);
        digitalWrite(relayPin12, LOW);
        digitalWrite(relayPin13, LOW);
        delay(5);// keep current flowing for a time measured in ms --- 1000 = 1s
        
        digitalWrite(relayPin7, LOW); // define new High or Low values for all pins (RESET operation)    
	digitalWrite(relayPin8, LOW);
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, HIGH);
        digitalWrite(relayPin11, HIGH);
        digitalWrite(relayPin12, HIGH);
        digitalWrite(relayPin13, LOW);
        delay(5); // keep current flowing for a time measured in ms
        }
}        
}
  while (1) // turn everything off and stay that way forever
        {             
        digitalWrite(relayPin7, LOW);  
	digitalWrite(relayPin8, LOW);
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, LOW);
        digitalWrite(relayPin11, LOW);
        digitalWrite(relayPin12, LOW);
        digitalWrite(relayPin13, LOW);
        }
}

In the second one I have 10,000 loops to perform (10*1,000) but I reduce the two delay times to 0.5 ms (5/10). This should also take 10 seconds but it only takes 1 second to complete.

//--- Digital Pins - use as many as u need 

int relayPin7 = 7;//these are the digital out pins on the arduino
int relayPin8 = 8;
int relayPin9 = 9;
int relayPin10 = 10;
int relayPin11 = 11;
int relayPin12 = 12;
int relayPin13 = 13;

void setup()
{   
	//--- Set up the  outputs   
   
        pinMode(relayPin7, OUTPUT);
        pinMode(relayPin8, OUTPUT);
        pinMode(relayPin9, OUTPUT);
        pinMode(relayPin10, OUTPUT);
        pinMode(relayPin11, OUTPUT);
        pinMode(relayPin12, OUTPUT);
        pinMode(relayPin13, OUTPUT);
	
	//--- open the serial port  - unneccessary, but lets you see output on computer screen
	Serial.begin(9600);
}

void loop()
{   
{
  for (int a=1; a <= 10 ; a++)//loops # times
{
  for (int i=1; i <= 1000; i++)//loops # times
	{  	     
        digitalWrite(relayPin7, HIGH); // Make all pins either High or Low (SET operation)
	digitalWrite(relayPin8, HIGH); 
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, LOW);
        digitalWrite(relayPin11, LOW);
        digitalWrite(relayPin12, LOW);
        digitalWrite(relayPin13, LOW);
        delay(5/10);// keep current flowing for a time measured in ms --- 1000 = 1s
        
        digitalWrite(relayPin7, LOW); // define new High or Low values for all pins (RESET operation)    
	digitalWrite(relayPin8, LOW);
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, HIGH);
        digitalWrite(relayPin11, HIGH);
        digitalWrite(relayPin12, HIGH);
        digitalWrite(relayPin13, LOW);
        delay(5/10); // keep current flowing for a time measured in ms
        }
}        
}
  while (1) // turn everything off and stay that way forever
        {             
        digitalWrite(relayPin7, LOW);  
	digitalWrite(relayPin8, LOW);
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, LOW);
        digitalWrite(relayPin11, LOW);
        digitalWrite(relayPin12, LOW);
        digitalWrite(relayPin13, LOW);
        }
}

In the third one I have 100,000 loops to perform (10*10,000) but I reduce the two delay times to 0.05ms (5/100). This should also take 10 seconds and now it does just as expected again.

//--- Digital Pins - use as many as u need 

int relayPin7 = 7;//these are the digital out pins on the arduino
int relayPin8 = 8;
int relayPin9 = 9;
int relayPin10 = 10;
int relayPin11 = 11;
int relayPin12 = 12;
int relayPin13 = 13;

void setup()
{   
	//--- Set up the  outputs   
   
        pinMode(relayPin7, OUTPUT);
        pinMode(relayPin8, OUTPUT);
        pinMode(relayPin9, OUTPUT);
        pinMode(relayPin10, OUTPUT);
        pinMode(relayPin11, OUTPUT);
        pinMode(relayPin12, OUTPUT);
        pinMode(relayPin13, OUTPUT);
	
	//--- open the serial port  - unneccessary, but lets you see output on computer screen
	Serial.begin(9600);
}

void loop()
{   
{
  for (int a=1; a <= 10 ; a++)//loops # times
{
  for (int i=1; i <= 10000; i++)//loops # times
	{  	     
        digitalWrite(relayPin7, HIGH); // Make all pins either High or Low (SET operation)
	digitalWrite(relayPin8, HIGH); 
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, LOW);
        digitalWrite(relayPin11, LOW);
        digitalWrite(relayPin12, LOW);
        digitalWrite(relayPin13, LOW);
        delay(5/100);// keep current flowing for a time measured in ms --- 1000 = 1s
        
        digitalWrite(relayPin7, LOW); // define new High or Low values for all pins (RESET operation)    
	digitalWrite(relayPin8, LOW);
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, HIGH);
        digitalWrite(relayPin11, HIGH);
        digitalWrite(relayPin12, HIGH);
        digitalWrite(relayPin13, LOW);
        delay(5/100); // keep current flowing for a time measured in ms
        }
}        
}
  while (1) // turn everything off and stay that way forever
        {             
        digitalWrite(relayPin7, LOW);  
	digitalWrite(relayPin8, LOW);
        digitalWrite(relayPin9, LOW);
        digitalWrite(relayPin10, LOW);
        digitalWrite(relayPin11, LOW);
        digitalWrite(relayPin12, LOW);
        digitalWrite(relayPin13, LOW);
        }
}

So the big question is... why is the 10,000 loop program seemingly either only performing 1,000 loops or performing 10,000 loops 10 times faster than I'm telling it to?

Your problems are due to integer arithmetic.

First, a variable declared as "int" can be no larger than 32767. If you define it to be "long" it can be about 32768 times larger than that.

Second, 5/10 is an integer divide and the result is zero.

Third, delay() works for integer delays only. If you want to delay for less than one millisecond, use delayMicroseconds(). But in general, you should avoid delays and use them only when absolutely necessary.

PatECake:
So the big question is... why is the 10,000 loop program seemingly either only performing 1,000 loops or performing 10,000 loops 10 times faster than I'm telling it to?

I'd be a bit surprised if the first one took exactly 10 seconds because you are only counting the delay() calls, not the 14 calls to digitalWrite() and the loop overhead.
The second and third ones are using delay(0) as explained above. The third one is roughly 10 seconds because that is how long it takes to call digitalWrite() 1,400,000 times. The second one, which calls digitalWrite() only 140,000 times should, therefore, take about 1 second.
The first one, with 14,000 calls to digitalWrite(), should take about 10.1 seconds because of delays.
You can measure and report the time the loop takes by adding:

unsigned long startTime = micros();
// put your loop here
unsigned long endTime = micros();
Serial.print(F("The loop took this number of microseconds: "));
Serial.println(endTime-startTime);

Awesome info guys! Thank you! I'm pretty new at this and just teaching myself as I go out of necessity. Your help is much appreciated!

Johnwasser, I stuck a couple of LEDs in my system so that I could visually see how long it was going and timed it with a wristwatch as a test for if it was working. Pretty far from that level of precision. It was just enough to convince myself that I was in the right order of magnitude. I knew the digitalwrite() things take time to do but I couldn't figure out how long that would be and in the end just hoped it would be negligible compared to my delay time.

jremington, I can use the delayMicroseconds() command like you said and get the job done but you seem hesitant to endorse that. Is there a better way to set these pins to one value, hold it there for a period of time, then set them to another value, hold it for a period of time, and repeat? What problems might I run into using the delay versus an alternate method that you may suggest?

To get better timing you can pick a set of seven I/O pins that are all in the same I/O register and write to them all at the same time using Direct Register Access. Unfortunately on the UNO the only port with seven pins exposed (PORTD) includes Pin 0 and Pin 1 so you can't do it in one register and still use Serial. It could be done with two registers (PORTD for pins 0-7 and PORTB for pins 8-13), making it slightly slower:

//--- Digital Pins - use as many as u need

const int relayAPin = 2; //  PORTD bit 2
const int relayBPin = 3; //  PORTD bit 3
const int relayCPin = 4; //  PORTD bit 4
const int relayDPin = 5; //  PORTD bit 5
const int relayEPin = 6; //  PORTD bit 6
const int relayFPin = 7; //  PORTD bit 7
const int relayGPin = 8; //  PORTB bit 0

void setup() {
  //--- Set up the  outputs

  pinMode(relayAPin, OUTPUT);
  pinMode(relayBPin, OUTPUT);
  pinMode(relayCPin, OUTPUT);
  pinMode(relayDPin, OUTPUT);
  pinMode(relayEPin, OUTPUT);
  pinMode(relayFPin, OUTPUT);
  pinMode(relayGPin, OUTPUT);

  // Serial.begin(9600);  CAN'T USE Serial BECAUSE WE NEED Pins 0 and 1
}

void loop() {
  {
    for (int a = 1; a <= 10 ; a++) //loops # times
    {
      for (int i = 1; i <= 10000; i++) //loops # times
      {
        PORTD |= 0b00001100;  //  Equivalent to:
        // digitalWrite(relayAPin, HIGH);
        // digitalWrite(relayBPin, HIGH);

        PORTD &= 0b00001111;  //  Equivalent to:
        // digitalWrite(relayCPin, LOW);
        // digitalWrite(relayDPin, LOW);
        // digitalWrite(relayEPin, LOW);
        // digitalWrite(relayFPin, LOW);

        PORTB &= 0b11111110;  //  Equivalent to:
        // digitalWrite(relayGPin, LOW);

        delayMicroseconds(5000 / 100); // keep current flowing for a time measured in ms --- 1000 = 1s

        PORTD &= 0d11100011;  //  Equivalent to:
        // digitalWrite(relayAPin, LOW);
        // digitalWrite(relayBPin, LOW);
        // digitalWrite(relayCPin, LOW);

        PORTD != 0b11100000;  //  Equivalent to:
        // digitalWrite(relayDPin, HIGH);
        // digitalWrite(relayEPin, HIGH);
        // digitalWrite(relayFPin, HIGH);

        PORTB &= 0b11111110;  //  Equivalent to:
        // digitalWrite(relayGPin, LOW);

        delayMicroseconds(5000 / 100); // keep current flowing for a time measured in ms
      }
    }
  }
  while (1) // turn everything off and stay that way forever
  {
    digitalWrite(relayAPin, LOW);
    digitalWrite(relayBPin, LOW);
    digitalWrite(relayCPin, LOW);
    digitalWrite(relayDPin, LOW);
    digitalWrite(relayEPin, LOW);
    digitalWrite(relayFPin, LOW);
    digitalWrite(relayGPin, LOW);
  }
}

Is there a better way to set these pins to one value, hold it there for a period of time, then set them to another value, hold it for a period of time, and repeat? What problems might I run into using the delay versus an alternate method that you may suggest?

The Arduino can't do anything else while it is waiting for delay() to finish. With a different approach, it is possible for it to do other things while waiting.

See the BlinkWithoutDelay example that comes with the program development package, and the post "Demonstration code for several things at the same time" .