First, take your code and press the Ctrl and T keys at the same time. That will reformat your code and make it easier to read. Next, I don't see where you set an initial value for loopCounter, but I assume it is 0. A for loop consists to 3 expressions:
for (epression1; expression2; expression3) {
// statements controlled by the for loop
}
Expression1 is usually an initialization statement. My guess is that you want to start ledCounter out at 0, so you might write:
for (int ledCounter = 0; expression2; expression3) {
// for loop statement body--statements controlled by the for loop
}
Expression1 is only executed once--the first time the for loop code starts to execute.
Expression2 is usually a logical test to determine if we need to make another pass through the statements controlled by the for loop. It appears that this should be the same expression that we see at the bottom of the do-while:
for (int ledCounter = 0; ledCounter < loopCounter; expression3) {
// statements controlled by the for loop
}
Somewhere in the code you did not post you probably set loopCounter to a value. (You should always post all your code so we don't have to guess.) I have no idea what the value should be , but lets set it to 6
and increment it each time after we turn on an LED.
Expression3 is usually used to change a variable that controls the number of passes made through the loop. So we will just increment it:
for (int ledCounter = 0; ledCounter < loopCounter; loopCounter++) {
// statements controlled by the for loop
}
Now look at the code:
if (randNumber == 2) {
ledArray[loopCounter] = 0;
ledCounter++;
}
if (randNumber == 3) {
ledArray[loopCounter] = 1;
ledCounter++;
}
if (randNumber == 4) {
ledArray[loopCounter] = 2;
ledCounter++;
}
if (randNumber == 5) {
ledArray[loopCounter] = 3;
ledCounter++;}
}
This looks very repetitive. Also, we know the random number is always going to be between 2 and 5 inclusively. Could we simplify all the statements above to:
ledArray[loopCounter] = randNumber - 2;
ledCounter++;
Walk through each possible value 2 through 5 and see if it doesn't yield the same results. Now we have something like:
for (int ledCounter = 0; ledCounter < loopCounter; expression3) {
randNumber = random(2, 6);
digitalWrite(randNumber, HIGH);
delay(500);
digitalWrite(randNumber, LOW);
delay(100);
ledArray[loopCounter] = randNumber - 2;
ledCounter++;
}
Since I can't see all of your code, you'll have to supply the missing definitions of the values for the variables and the arrays.
Edit: WHen control reaches the closing brace at the bottom of the for loop statement body, control is sent to expression3. Upon processing expression3, control is immediately transferred to expression2 to see if we need to go through the loop again. Note expression1 is not visited again.