Skip to main content

Exploring Calculus with Maple Introductory Calculus

Section 18.2 for/while Loops

‘For’ loops allow us to carry out a computation repeatedly for an entire range of values. We can also combine these loops with conditional statements like ‘if’ and ‘else’, as we will see in Section 18.3. To use a ‘for’ loop, we are required to type
> for i from a to b do
    ...
    ...
  end do
where \(i\) is a “dummy variable”, referred to as an index. On the first iteration of the loop, the index begins at \(a\text{.}\) At the end of each iteration, the index is incremented by one. In the last iteration, the index will be equal to \(b\) and the loop will terminate. This means that the lines of code within the loop will be executed a set number of times.

Example 18.5. Outputting the First \(n\) Derivatives.

We will use a basic ‘for’ loop to output the first \(10\) derivatives of the function \(f(x)=\sin(2x)\text{.}\) To do this, we will use the diff() command within the loop. The ‘for’ loop will output the \(k\)th derivative, starting from \(k=1\) and ending at \(k=10\text{.}\)

Aside

> f(x) := sin(2*x);
\begin{equation*} \displaystyle f\, := \,x\mapsto \sin \left( 2\,x \right) \end{equation*}
> for k from 1 to 10 do
    diff(f(x), x$k)
  end do
\begin{equation*} \displaystyle 2\,\cos \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle -4\,\sin \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle -8\,\cos \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle 16\,\sin \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle 32\,\cos \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle -64\,\sin \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle -128\,\cos \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle 256\,\sin \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle 512\,\cos \left( 2\,x \right) \end{equation*}
\begin{equation*} \displaystyle -1024\,\sin \left( 2\,x \right) \end{equation*}
Adding a while is a short way of adding a condition to the for loop. This additional condition may terminate the loop early once its condition is no longer met.

Example 18.6. Summing Squares Using a While Loop.

In this example, let’s say we want to add the first few squares together:
\begin{equation*} 1^2+2^2+3^2+\cdots+i^2 \end{equation*}
until \(i^2\) becomes greater than or equal to \(100\text{.}\) We can make this a while condition on the loop: \(i^2 < 100\text{.}\) This condition will be checked for every iteration on the loop and the moment that this condition is no longer met, the loop terminates.
The loop itself will increment \(i\) for each iteration. We will need to assign an initial total of zero and add the value of \(i^2\) to it each iteration, reassigning the updated value.
> total := 0:
> for i from 1 while i^2 < 100 do
    total := total + i^2
  end do
\begin{equation*} \displaystyle \textit{total}\, := \,1 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,5 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,14 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,30 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,55 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,91 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,140 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,204 \end{equation*}
\begin{equation*} \displaystyle \textit{total}\, := \,285 \end{equation*}
\begin{equation*} \displaystyle \end{equation*}
The updated value of the total is displayed at the end of each iteration and the overall sum is
\begin{equation*} 1^2+2^2+3^2+4^2+\cdots+7^2+8^2+9^2=285\text{.} \end{equation*}
This example could easily be accomplished using a simple sum() command, although it is useful to understand how to use a for/while loop.