for loopsnew, which we'll see later on
is also used whenever we create any new object in a program.
The first item in array has index 0, so the above expression would access the
twelfth element in the numbers array.
= assignment operator with the position index again in
brackets:
{}:
new (because the
expression between the curly brackets actually creates the array) and don't need
to specify the size of the array (because this is given by the number
of elements in the expression between the curly brackets). Note also that
this is only possible when the array is first declared. We can't do
something like the following:
for loopsfor loop:
for body are executed.
After the statements in the body are executed, the
increment statement is executed.
Then the condition is evaluated again.
If it is true, the statement in the body is evaluated again.
This process repeats until the condition becomes false.
Here is an example with an array; for is often used to
access and/or assign the elements in an array.
This example uses an assignment operator that we haven't seen before, ++.
This operator increments the value of an integer variable by 1.
So index++; is the same as index = index + 1;
length, which
each variable automatically has.
For example, if the variable animals is bound to an array of animals,
we can know how many positions the array has with the expression
animals.length.