You are on page 1of 1

Laboratory Exercise No.

6
Repetition Structures in MATLAB

1. Objective:
The activity aims to write and use for-end and while-end loops.

2. Intended Learning Outcomes (ILOs):


The students shall be able to:
1. Write and use for-end loops
2. Write and use while-end loops

3. Discussion :
In order to create any computer program, the organization of the statements that make it must
be given due consideration. The sections of the MATLAB code or any computer code for that
matter are classified into sequences, selection structures and repetition structures.

Loops is the other name for repetition structures and it has five basic parts that includes:
1. a parameter that serves as a way to end the loop or not
2. starting value for the parameter
3. in each time of the loop there should be a way to change the parameter which provides a
means to stop the execution of the repetition
4. to decide when to end the repetition there should be a comparison to a criterion using the
parameter
5. there should be a calculation inside the repetition structure

There are two different types of loops that are being supported by matlab that includes:
a. the for loop and
b. the while loop

Midpoint break loop is the third type of loop that makes use of two additional commands which
are break and continue. Whenever the number of times to repeat the loop is known the for
loop is
the easiest to use. Whenever there is a need to keep repeating the instructions until a criterion
is
met the while loop is the easiest to use. Whenever the commands in the loop is
executed at least once but the decision to exit the loop is based on some criterion the midpoint
break loop is the easiest to use.

Matlab programs can be composed that avoid loops by using either the find command or by
vectorizing the code which means that the entire vectors are operated at a time instead of
one element at a time. Because vectorized programs run faster and requires few
programming steps. It is a good idea to avoid loops if possible.

The for loop structure is fairly simple. The starting point of the for loop is the command for
followed by the index that determines when to end the repetitions. This index changes for
every time it passes through the loop. The group of commands which are to be executed
follows the starting line. And the command end provides the ending of the loop. The structure
is:

for index = [ matrix ]


commands here
end
The format for a while loop is
while criterion
Commands here
end 6.1.1 T I P V P A
A 0 5 4 D
While loops continue until some criterion is met. Revision Status/Date:
0/2009 September 09

You might also like