Skip to main content
Library homepage
Engineering LibreTexts

3.4.1: for Loop Supplemental Material

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    By Carey A. Smith

    Structure of a for loop

    Every for loop must start with the key word "for" followed by the loop index and its values.

    Then the body of the for loop is whatever code your want.

    Then the keyword "end" specifies the end of the loop code.

    This is a simple example:

    for kk = 1:4

    x = cos(kk*pi/16);

    y = sin(kk*pi/16);


    For good style, vertically align the "for" and "end". Indent the body of the for loop.

    You may use the index (kk in this example) in the calculations, as shown in this example. But never modify the loop index inside the for loop. MATLAB will increment the index automatically.

    Watch these videos for additional instruction on for loops:

    Video: Looping structures in MATLAB: Basic FOR loops, Robert Talbert

    Video: MATLAB For Loop Tutorial, Ilya Mikhelson

    Here are 2 more examples of for loops:


    Figure \(\PageIndex{i}\): Factorial for loop flowchart
    Factorial Example for loopExample \(\PageIndex{1}\)

    The factorial of a number N in algebra is defined as:

    N! = 1*2*3*...*N

    This can be computed in MATLAB/Octave using a for loop using the logic described in accompanying figure.


    The MATLAB code for this is:

    % Input (precondition): N = integer >= 2
    % Output: FN
    FN = 1; % Starting value
    for k = 2:N % k=index, kstart : to kend
        FN = FN*k


    Exercise \(\PageIndex{1}\) Compute x^n using a for loop

    Compute x^n these 2 ways:

    1a. Set x = 2

    1b. Set n = 7

    1c. Compute a = x^n.

    2a. Set result = 1

    2b. Code a for loop that multiplies result by x, n times.

    2c. Use this line inside the for loop:

    result = result*x


    x = 2 % Initialze x
    n = 7 % Initialze n (the power)
    result = 1 % Initialze the result
    for k=1:n
        result = result*x


    This page titled 3.4.1: for Loop Supplemental Material is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

    • Was this article helpful?