Learn to Program with ZedLX

Free Online Programming Course for Beginners

Arrays [3/11]

The getter .n returns the number of elements in an array.

A for loop can be used to iterate over all elements of an array. The loop variable can be used as an index to access each element.

Note that the ++ operator is used in the for loop to ensure that the loop counts upwards. The potential issue is that an array can be empty, which would cause the getter .n to return zero, and the loop to try iterating backwards.

Loading...