for
A for
loop expects three statements separated by
semicolons as arguments. The first statement is executed first, and
only once. It is most frequently used to assign a value to a
variable, which is usually used by the loop to perform iteration. The
second statement is a condition that should return
true for the loop to continue looping. The third
statement is invoked by the for loop after every
iteration, and is used to increment or decrement the iteration
variable. For example, the following for loop
prints all the values of the array myports:
for(i=0; i < max_index(myports); i++)
{
display(myports[i],"\n");
}The function max_index()
returns the number of elements in an
array, and we use it in our
for loop to ensure that the value of
i is within range.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access