You are on page 1of 13

The for Loop

The for statement, or the for loop, is used when it is necessary to repeat
statement(s) in a script or function, and when it is known ahead of time how
many times the statements will be repeated. The statements that are
repeated are called the action of the loop.

The variable that is used to iterate through values is called a loop variable, or
an iterator variable. For example, the variable might iterate through the
integers 1 through 5 (e.g., 1, 2, 3, 4, and then 5).
Finding Sums and Products
A very common application of a for loop is to calculate sums and
products.
Sums and Products with Vectors

function to sum all the elements in a vector.


Combining for Loops with if Statements
Nested for Loops
The action of a loop can be any valid statement(s). When the action of a
loop is another loop, this is called a nested loop.
Nested Loops and Matrices

You might also like