# Basic Matlab for loop

``````A=2;
for x=0:2:4
A=[A, A*x];
end
A
``````

I'd appreciate any help! The for loop condition as well as the 3rd line and how they work together I can't quite piece together

On

So, here comes the walktrough.

``````A = 2;
``````

`A` is an array of length `1`, with `2` as the only element.

``````for x = 0:2:4
``````

Have a look at the Examples section of the `for` help. You create an "iteration variable" `x`, which iterates through an array with the values `[0, 2, 4]`. See also the Examples section of the `:` operator help.

``````A = [A, A*x];
``````

Concatenate array `A` with the value of `A*x` (multiplying an array with a scalar results in an array of the same length, in which each element is multiplied by the given scalar), and re-assign the result to `A`. See also the help on Concatenating Matrices.

• Initially, `A = [2]`.
• For `x = 0`: `A = [[2], [2] * 0]`, i.e. `A = [2, 0]`.
• For `x = 2`: `A = [[2, 0], [2, 0] * 2]`, i.e. `A = [2, 0, 4, 0]`.
• For `x = 4`: `A = [[2, 0, 4, 0], [2, 0, 4, 0] * 4]`, i.e. `A = [2, 0, 4, 0, 8, 0, 16, 0]`.
``````end
``````

End of `for` loop.

``````A
``````

Output content of `A` by implicitly calling the `display` function by omitting the semicolon at the end of the line, see here for explanation.