I'll be specific here, you need to read on basic programming in c++, because you are questioning the fundamental ways the for-loop in c++ works.
I will start by formatting your code so that is is easier to read:
{
int = myArray[5];
for (int x = 0; x < 5; x++)
{
myArray[x] = 42;
cout << x << ":" << myArray[x] << endl;
}
}
Let me point out some errors in your code. Firstly, when you declare a variable in c++, you must also declare its data type along with it. So the following example:
var = 2;
Is invalid, because the data type of the variable var is not specified. The compiler cannot tell what the data type of a variable is by simply looking at the value passed to it in c++. Therefore, we need to add the data type just before the name of the variable when DCLARING IT ONLY. So in the above example:
int var = 2;
Is a valid variable declaration because we have specified that this variable will only store an int value.
Now the problem in your code is that you are trying to do:
int = myArray[5];
This makes no sense. First problem is that you are trying to assign a data type a value as if it were a variable. Second problem is that you never declared myArray before. From your cod, I'm assuming that you intended to do:
int myArray[5];
This makes sense because your for-loop also runs 5 times. In this line, you have declared an array called myArray that stores 5 values of type int.
Now moving on to the for-loop. Lets dissect the header statement of the for-loop:
for (int x = 0; x < 5; x++)
The first statement right after the opening semicolon int x = 0;
simply declares a variable ONCE for the entire run of the for loop. This variable x is local to the for loop and cannot ba accessed outside of it. In c++, we refer to it as a counter variable as it stores the number of times we have gone looped through a for-loop.
The next statement x < 5;
is a condition for the for-loop. While this condition is true, the for-loop will run; In your case, while x is less than 5, it will run through the loop. As soon as this condition become false; in this code, if x becomes equal to or greater than 5, then the condition is falsified, and the loop is terminated. This way, we ensure that the loop runs through our code only the number of times we want it to.
The final statement x++;
simply means that x is incremented at every point. If it is not incremented, then the value of x will never change, and the condition of the loop will never be falsified, making it an infinite loop. Therefore, this statement is crucial.
Now while we are inside the for-loop, we can use the variable x. This is particularily useful in your case as you have an array, and you are trying to fill it up with values. Instead of doing array[a]=n, where a ranges from 0 up till the size of the array minus 1, and n is a random number, we can use a for loop to initialize our values.
In the for loop, how do we do that? We use the counter x, that increments itself after every loop run, to serve as the "a" in array[a]. That's why we use myArray[x] = 42;
, simply because x increments itself by 1 after every loop and this number can be used as the address (sorry for my poor choice of words here) of the array. Why it allocates the number 42 is random; it could allocate any number.
If you still need some clarification, or if I made a mistake in my post, please let me know in the comments box.