Programming Tutorial > Task 3–1: Writing For Loops
  
Task 3–1: Writing For Loops
Use for loops to modify several elements in a vector using a single expression.
1. Define a function f that accepts a variable vec and create a new program.
2. To define an empty for loop, on the Math tab, in the Operators and Symbols group, click Programming, and then click for.
* 
You cannot modify the operator ∈ (belongs to). This operator is a part of the for statement syntax.
3. Specify an iterator i and a range of values (0..2) as shown below.
* 
The range of values can also be a single value, a vector, or a matrix defining the range of values.
The default origin of a vector or a matrix in PTC Mathcad is 0.
4. Assign the value of variable i to the first three elements of the vector as shown below.
5. Evaluate function f when vec has two rows of strings.
The result is a three row vector, as dictated by variable i. The original two rows are over-written by the for loop.
6. Evaluate function f when vec has three rows.
The result is a three row vector, as dictated by variable i. The original three rows are over-written by the for loop.
7. Evaluate function f when vec has five rows.
The result is a five row vector where the first three rows are set by the for loop and the last two rows remain unchanged.
Proceed to Task 3–2.