<meta http-equiv="refresh" content="1; url=/nojavascript/"> Introduction to For Loops | CK-12 Foundation
Dismiss
Skip Navigation
You are reading an older version of this FlexBook® textbook: Engineering: An Introduction to Solving Engineering Problems with Matlab Go to the latest version.

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 1.0 to 2.0 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 1.0 to 2.0.)

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 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
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 C 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

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 = n-1;
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 f cycles/second is

y = \cos (2\pi ft)

Create an m-file script to plot the cosine waveform with frequency f = 2 cycles/s for values of t between 0 and 4.

Exercise 8

Suppose that we wish to plot (on the same graph) the cosine waveform in Exercise 7 for the following frequencies: 0.7, 1, 1.5, and 2. 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 r, and the rotations per second of the wheel as w. The robot speed s (in inches/s) is related to r and w by the equation

s = 2\pi rw

On one graph, create plots of the relationship between s and w for values of r of 0.5 in, 0.7 in, 1.6 in, 3.2 in, and 4.0 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 c of this triangle for several combinations of side lengths a and b; the specific combinations of a and b are given in Table 1. Write an m-file to do this.

Side lengths
a b
1 1
1 2
2 3
4 1
2 2

Image Attributions

Description

Authors:

Categories:

Grades:

Date Created:

Feb 23, 2012

Last Modified:

Apr 29, 2014
You can only attach files to None which belong to you
If you would like to associate files with this None, please make a copy first.

Reviews

Please wait...
Please wait...
Image Detail
Sizes: Medium | Original
 
CK.ENG.ENG.SE.1.Engineering-Matlab.6.1

Original text