You are on page 1of 7

Chapter-6

LOOPS
In a loop, the execution of a command, or a group of commands, is repeated several times
consecutively. Each round of execution is called a pass.

MATLAB has two kind of loops.

for-end loops:

for k=f:s:t
-------------
-------------MATLAB commands
-------------
-------------

end (The value of k in the last press)

(The increment in k after each pass)

(The value of k in the first pass)

(loop index variable)

In for-end loops the number of passes is specified when the loop starts.

As we have seen in the structure of a for-end loop. In the first pass k=f and the computer executes the
command between the for and the end commands, Then the program goes back to the for command
for the second pass. s is added in the value of k, and the commands between the for and the end
commands are executed with the new value of k. The program repeats itself until the last pass Where k
= t, Then the program does not go for the next pass to the for, but continues with the commands that
follow the end command. For example, if k=1:3:10, there are four loops and value of k in each loop is
1,4,7,10.
The increment s can be negative (i.e k = 25:-5:10 produces four passes with k = 25,20,15 and 10).
If the value of k, s and t are such that k cannot be equal to t, then if s is positive, the last pass is
one where k has the largest value that is smaller than t (i.e k = 8:10:50 produces five passes with
k = 8,18,28,38,48).
In the for command k can also be assigned specific value (typed in as a vector). for k = [7 9 -1 3 3
5].

A simple example of for-end loop in command window.

Q) A vector is given by V = [5 17 -3 8 0 -7 12 15 20 -6 6 4 -2 16]. write a program as a script file that


doubles the element that are positive and are divisible by 3 or 5 and takes the cubes of the elements
that are negative but greater than -5 and finally prints the modified vector V?

clc
close all
clear all
V = [5 17 -3 8 0 -7 12 15 20 -6 6 4 -2 16];
n = length(V);
for i=1:n
if(V(i)>0)&&((rem(V(i),3)==0)||(rem(V(i),5)==0))
V(i) = 2*V(i);
elseif (V(i)<0)&&(V(i)>-5)
V(i) = (V(i))^3;
else
V(i) = V(i);
end
end
V

While-end loops:

While-end loops are used in those situations when loop is required but the number of passes is not
known ahead of time. The looping process continues until a stated condition is satisfied. The structure of
while-end loop is shown in figure below.

While conditional expression


---------
---------MATLAB commands
---------
end
The first command is a while statement that includes a conditional expression. When the
program reaches this command the conditional expression is checked. If it is false, MATLAB goes
to the end statement and continues with the program. If the conditional expression is true,
MATLAB executes the commands that follow between the while and end command. Then
MATLAB comes back to check the conditional expression once again. This looping process
continues until the conditional expression is false.
The variables in the conditional expression must have assigned values when the MATLAB
executes the while command for the first time.
At least one of the variables in the conditional expression must be assigned a new value in the
commands that are between while and end commands. Otherwise once the looping starts it
will never stop since the conditional expression will remain true.

>> x = 2;
>> while x<=96
x = x^2% no semicolon, value of x will be displayed
end

x=

x=

16

x=

256

When writing a while-end loop, the programmer has to be sure that the variables that are in
conditional expression and are assigned new value during the looping process will eventually be
assigned new values in each looping process that make the conditional expression in the while
command false. Otherwise the loop will continue indefinitely (indefinite loop).
A situation of indefinite loop can occur in spite of careful programming. User can stop the
execution of an indefinite loop by pressing the Ctrl + C or Ctrl + Break keys.

NESTED LOOP:
Fig 6.1 shows structure of a for-end loop within another for-end loop
for k = 1:n
for h = 1:m
------
------A group of MATLAB commands Nested loop
------ loop
------
end
end

In the above nested loop, Every time K increases by 1, the nested loop executes m times. Overall
the group of commands are executed n*m times.
In the loop shown in above figure, If n=5 and m=5, then first k =1 and the nested loop executes
five times with h = 1,2,3,4 and 5. Next k=2 and the nested loop executes again five times with h
= 1,2,3,4 and 5. Lastly k=5 and the nested loop executes again five times.

CREATING A MATRIX WITH A LOOP

Q) Use loops to create a 4*7 matrix in which the value of each element is the sum of its indices (the row
number and column number of the element). For example, the value of element is A (2,5) is 7?
clc
clear all
close all
m = input('number of rows in matrix');
n = input('number of column in matrix');
A = [];
for i=1:m
for j=1:n
A(i,j) = i + j;%each element is sum of its indices(row number +
column number)
end
end
A%for displaying matrix A

*link for downloading program: matrix.m from folder matlabfiles

Q) Write a program in MATLAB that creates a matrix with elements that have the following values. The
value of elements in the first row is the number of column. The value of element in the first column is
the number of row. The rest of the elements are equal to the sum of the elements above them and the
elements to the left. When executed, the program asks the user to enter values for n and m?
A = [];
m = input('enter number of rows in the matrix');
n = input('enter number of column in the matrix');
for i=1:m
for j=1:n
if(i==1)
A(i,j) = j;%value of element in first row is equal to number of column
elseif(j==1)
A(i,j) = i;%value of element in first column is equal to number of row
else
A(i,j) = 0;
for p=1:i-1
A(i,j) = A(i,j) + A(p,j);%sum of all element that are above the element
end
for q=1:j-1
A(i,j) = A(i,j) + A(i,q);%sum of all element that are to the left of element
end
end
end
end
A%for displaying matrix

Q) Write a program in a script file that takes the matrix and print the matrix in reverse order?
%enter the square matrix and print the element of matrix in reverse order
clc
clear all
close all
m = input('enter square matrix');
b = size(m);
for i=1:b(1)
for j=1:b(2)
c(i,j) = m((b(1)-(i-1)),(b(2)-(j-1)));%for finding value of each
element of reversed matrix
end
end
c
%for displaying reversed matrix

The break command:


When inside a loop (for & while), the break command terminates the execution of the loop (the
whole loop, not just the last pass). When the break command appears in a loop, MATLAB jumps
to the end command of the loop and continues with the next command (does not go back to the
for command of that loop).
If the break command is inside a nested loop, only the nested loop is terminated.
When a break command appears outside a loop in a script, or function file, it terminates the
execution of the file.

Q) Write a program in a script file to test whether a number is prime or composite or neither prime nor
composite?

clc
close all
clear all
N = input('enter the number to be tested');
k = 0;
if(N~=1)
for i=2:N-1
if(rem(N,i)==0)
k = 1;%if remainder becomes zero,no. is composite and there is no
need to test further,
break%it comes out of loop and displays number is composite,therefore
break command is used
end
end
if(k==1)
disp('number is composite');
else
disp('number is prime');
end
else
disp('number is neither prime nor composite');
end

Q) Write a program in a script file to print first N prime number or program which takes value of N and
print first N natural number?

%print first N prime numbers


clc
clear all
close all
N = input('enter the number of prime numbers to be printed');
s = 0;
a = 1;
while(s<N)
a = a + 1;
k = 0;
for i=2:a-1
if(rem(a,i)==0)
k = 1;
break
end
end
if(k~=1)
s = s + 1;
m(s) = a;
fprintf('%f\t',m(s));
end
end

Q) Write a program to enter the size of vector and to enter the elements of vector one by one. Using
conditional statements and loops write a program that calculates the sum of positive elements in the
vector?
n = input('enter the size of vector');
for i=1:n
a(i) = input('enter elements of vector');
end
s = 0;
for i=1:n
if(a(i)>0)
s = s + a(i);
end
end
fprintf('sum of elements of vector is %f',s);

You might also like