Arrays - Using for loop to iterate through array explaination

Hi there, I am fairly new to C++. I am wondering why in my for loop I use i <= (ARRAY_SIZE -1); To me I read it as: 0 less than or equal too (3-1). Also when I display my elements, I use words[i]. Can someone please explain how and why this works. Any feedback would be greatly appreciated! Thanks in advance! Eagle

const int ARRAY_SIZE = 3;

string words[ARRAY_SIZE];

words[0] = "April";
words[1] = "May";
words[2] = "June";

for (int i = 0; i <= (ARRAY_SIZE - 1); i++)

(cout << words[i] << endl);
}
That is equivalent to a more common:
for (int i = 0; i < ARRAY_SIZE; i++)

In both cases the i is initialized to 0.
One iteration succeeds, because 0 < 3 (or 0 <= 2) is true.
Second iteration succeeds, because 1 is still a small number.
Third time i=2. 2 is still less than 3, or in your version 2 is less than equal to 3.

Fourth time. 3 is not longer less than 3, nor equal to 2, so there will be no fourth iteration.
Thanks keskiverto!
Topic archived. No new replies allowed.