6.1: Introduction to For Loops
The for loop is one way to repeat a series of computations using different values. The for loop has the following syntax:

for d = array

% Command 1

% Command 2

% and so on


end
In the for loop,
array
can be any vector or array of values. The for loop works like this:
d
is set to the first value in
array
, and the sequence of commands (Command 1, Command 2, and so on) in the body of the for loop is executed with this value of
d
.
Then
d
is set to the second value in array, and the sequence of commands in the body of the for loop is executed with this value of
d
.
This process continues through all of the values in array. So a for loop that performs computations for values of
d
from \begin{align*}1.0\end{align*} to \begin{align*}2.0\end{align*} is:

for d = 1.0:0.05:2.0

% Command 1

% Command 2

% and so on


end
(Recall that
1.0:0.05:2.0
creates a vector of values from \begin{align*}1.0\end{align*} to \begin{align*}2.0.\end{align*})
Note that in all of the examples in this module, the commands inside the for loop are indented relative to the
for
and
end
statements. This is not required, but is common practice and makes the code much more readable.
The flow of control through a for loop is represented by the flow chart in Figure 1. This flow chart graphically shows how the sequence of commands in the for loop is executed once for each value. The flow of control through the for loop is also represented by the pseudo code in Figure 2; note that the pseudo code looks very similar to the actual mfile code.
A flow chart containing a for loop.

for each element of the vector
 Do Command 1
 Do Command 2
 and so on
Figure 2: Pseudo code for a for loop.
A useful type of for loop is one that steps a counter variable from 1 to some upper value:

for j = 1:10

% Commands


end
For example, this type of loop can be used to compute a sequence of values that are stored in the elements of a vector. An example of this type of loop is

% Store the results of this loop computation in the vector v

for j = 1:10

% Commands

% More Commands to compute a complicated result

v(j) = result;


end
Using a for loop to access and manipulate elements of a vector (as in this example) may be the most natural approach, particularly when one has previous experience with other programming languages such as \begin{align*}C\end{align*} or Java. However, many problems can be solved without for loops by using the builtin vector capabilities. Using these capabilities almost always improves computational speed and reduces the size of the program. Some would also claim that it is more elegant.
For loops can also contain other for loops. For example, the following code performs the commands for each combination of
d
and
c
:

for d=1:0.05:2

for c=5:0.1:6

% Commands


end


end
For Loop Drill Exercises
Exercise 1
How many times will this program print "Hello World"?

for a=0:50

disp('Hello World')


end
Exercise 2
How many times will this program print "Guten Tag Welt"?

for a=1:1:50

disp('Guten Tag Welt')


end
Exercise 3
How many times will this program print "Bonjour Monde"?

for a=1:1:50

disp('Bonjour Monde')


end
Exercise 4
How many times will this program print "Hola Mundo"?

for a=10:10:50

for b=0:0.1:1

disp('Hola Mundo')


end


end
Exercise 5
What sequence of numbers will the following for loop print?

n = 10;

for j = 1:n

n = n1;

j


end
Explain why this code does what it does.
Exercise 6
What value will the following program print?

count = 0;

for d = 1:7

for h = 1:24

for m = 1:60

for s = 1:60

count = count + 1;


end


end


end


end

count
What is a simpler way to achieve the same results?
For Loop Exercises
Exercise 7
Frequency is a defining characteristic of many physical phenomena including sound and light. For sound, frequency is perceived as the pitch of the sound. For light, frequency is perceived as color.
The equation of a cosine wave with frequency \begin{align*}f\end{align*} cycles/second is
\begin{align*}y = \cos (2\pi ft)\end{align*}
Create an mfile script to plot the cosine waveform with frequency \begin{align*}f = 2\end{align*} cycles/s for values of \begin{align*}t\end{align*} between and \begin{align*}4\end{align*}.
Exercise 8
Suppose that we wish to plot (on the same graph) the cosine waveform in Exercise 7 for the following frequencies: \begin{align*}0.7\end{align*}, \begin{align*}1\end{align*}, \begin{align*}1.5\end{align*}, and \begin{align*}2\end{align*}. Modify your solution to Exercise 7 to use a forloop to create this plot.
Exercise 9
Suppose that you are building a mobile robot, and are designing the size of the wheels on the robot to achieve a given travel speed. Denote the radius of the wheel (in inches) as \begin{align*}r\end{align*}, and the rotations per second of the wheel as \begin{align*}w\end{align*}. The robot speed \begin{align*}s\end{align*} (in inches/s) is related to \begin{align*}r\end{align*} and \begin{align*}w\end{align*} by the equation
\begin{align*}s = 2\pi rw\end{align*}
On one graph, create plots of the relationship between \begin{align*}s\end{align*} and \begin{align*}w\end{align*} for values of \begin{align*}r\end{align*} of \begin{align*}0.5\end{align*} in, \begin{align*}0.7\end{align*} in, \begin{align*}1.6\end{align*} in, \begin{align*}3.2\end{align*} in, and \begin{align*}4.0\end{align*} in.
Exercise 10
Sides of a right triangle.
Consider the right triangle shown in Figure 3. Suppose you wish to find the length of the hypotenuse \begin{align*}c\end{align*} of this triangle for several combinations of side lengths \begin{align*}a\end{align*} and \begin{align*}b\end{align*}; the specific combinations of \begin{align*}a\end{align*} and \begin{align*}b\end{align*} are given in Table 1. Write an mfile to do this.
\begin{align*}a\end{align*}  \begin{align*}b\end{align*} 

\begin{align*}1\end{align*}  \begin{align*}1\end{align*} 
\begin{align*}1\end{align*}  \begin{align*}2\end{align*} 
\begin{align*}2\end{align*}  \begin{align*}3\end{align*} 
\begin{align*}4\end{align*}  \begin{align*}1\end{align*} 
\begin{align*}2\end{align*}  \begin{align*}2\end{align*} 
Notes/Highlights Having trouble? Report an issue.
Color  Highlighted Text  Notes  

Please Sign In to create your own Highlights / Notes  
Show More 