Skip to main content
Engineering LibreTexts

7.8: 'while' Loops

  • 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}}\)

    We review for loops and introduce while loops in this section.

    Suppose we want to have part of our program re-run a preset number of times. For this, we use a for loop. The basic format of this structure is:

    for (put counter conditions here)
    (put calculations here)

    Example 7.4.1: Comparing 'for' and 'sum'

    Let’s compare two methods for adding up the first five integers. Using the sum command we can use

    >> sum(1:5)
    ans =

    Now, using a for loop to create a cumulative sum:

    totalsum=0; % initialize
    for i=1:5

    The variable totalsum will have value 15.

    In example 7.4.1, the for loop was the long way of doing the problem (and therefore stresses the power of the sum function), but the following example shows a more in-depth for loop.

    Example 7.4.2: Using 'for'

    Let’s find the first 10 Fibonacci numbers using the recursive definition.

    %initialize the matrix

    for i=3:10


    The output for this would be

    A =
    0 1 1 2 3 5 8 13 21 34

    Now, suppose want to have part of our program run until a certain condition is met, even though we may not know how many times the loop will need to run until that happens. For this, we use a while loop.

    The basic format of this structure is:

    while (put conditions for the loop to keep running)
        (put calculations here)

    Let’s rewrite the last example with a slight twist. Let’s find the Fibonacci numbers until they exceed 1000.

    Example 7.4.3: Using 'while'

    %Initialize the matrix. (We don't know how big it will be,
    % so we will grow it in the while loop.)

    j=2; %initialize a counter
    while A(j) < 1000
        j=j+1; %move the counter along


    The output for this would be

    A =
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

    The loop concludes when we pass 1000.


    When using a while loop, avoid coding an infinite loop. If an infinite loop occurs, you can stop the program by pressing Ctrl-C.

    This silent video is helpful to understand the use of break, continue, and return in while and for loops.

    Break and Continue Statements in Matlab


    This page titled 7.8: 'while' Loops is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.