<img src="https://d5nxst8fruw4z.cloudfront.net/atrk.gif?account=iA1Pi1a8Dy00ym" style="display:none" height="1" width="1" alt="" />
Skip Navigation
Our Terms of Use (click here to view) have changed. By continuing to use this site, you are agreeing to our new Terms of Use.

6.1: Introduction to For Loops

Difficulty Level: At Grade Created by: CK-12
Turn In

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

In the for loop,


can be any vector or array of values. The for loop works like this:


is set to the first value in


, 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





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



This process continues through all of the values in array. So a for loop that performs computations for values of


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

(Recall that


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




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 m-file code.

A flow chart containing a for loop.

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

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;

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 built-in 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





for d=1:0.05:2
for c=5:0.1:6
% Commands

For Loop Drill Exercises

Exercise 1

How many times will this program print "Hello World"?

for a=0:50
disp('Hello World')

Exercise 2

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

for a=-1:-1:-50
disp('Guten Tag Welt')

Exercise 3

How many times will this program print "Bonjour Monde"?

for a=-1:1:-50
disp('Bonjour Monde')

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')

Exercise 5

What sequence of numbers will the following for loop print?

n = 10;
for j = 1:n
n = n-1;

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;

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 m-file 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 \begin{align*}0\end{align*} 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 for-loop 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.

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 m-file to do this.

Side lengths
\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 to create your own Highlights / Notes
Show More

Image Attributions

Show Hide Details
Date Created:
Feb 23, 2012
Last Modified:
Apr 13, 2016
Save or share your relevant files like activites, homework and worksheet.
To add resources, you must be the owner of the section. Click Customize to make your own copy.
Please wait...
Please wait...
Image Detail
Sizes: Medium | Original