You are on page 1of 15

BIL113E – Introduction to Scientific and

Engineering Computing (MATLAB)

Assoc. Prof. Tuncay ÖZCAN


Management Faculty
Department of Management Engineering
tozcan@itu.edu.tr
Chapter V.
LOOPS IN MATLAB - I
Loops
 A loop is another method to alter the flow of a computer
program. 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. In each pass at least
one variable, but usually more than one, or even all the variables
that are defined within the loop are assigned new values.
 MATLAB has two kinds of loops: for-end and while-end
 In for-end loops the number of passes is specified when the
loop starts.
 In while-end loops, the number of passes is not known ahead
of time, and the looping process continues until a specified
condition is satisfied.

1-3
for-end loops
In for-end loops the execution of a command, or a group of
commands, is repeated a predetermined number of times. The
form of the loop is as follows:
for loop variable=first value:increment:last value
..... A group of
..... MATLAB commands
end
For example, if for i=1:2:9, there are five loops, and the value of
i in each loop is 1, 3, 5, 7, and 9
The loop index variable can have any variable name ( i, j, k,
counter...)
Each for command in a program must have an end command.
for-end loops
For example; let for i=a :b :c.
In the first iteration, i=a and the computer executes the commands
between the for and the end commands. Then, the program goes
back to the for command for the second iteration. i obtains a new
value equal to i=a+b, and the commands between the for and the
end commands are executed with the new value of i. The process
repeats itself until the last iteration where i=c. Then, the program
does not go back to the for, but continues with the commands
that follow the end command.
The increment b can be negative (i=25:–5:10 produces four
iterations with i= 25, 20, 15, 10). If the increment value is omitted,
the value is 1 (default) (i.e. for i=3:7 produces five iterations with
i= 3, 4, 5, 6, 7).
While-end loops
The form of the loop is as follows:
while conditional expression
..... A group of
..... MATLAB commands
end
while-end loops are used in situations when looping is needed
but the number of iterations is not known ahead of time. In
while-end loops the number of passes is not specified when the
looping process starts. Instead, the looping process continues until
a stated condition is satisfied.
The conditional expression in the while command must include
at least one variable.
While-end loops
The first line is a while statement that includes a conditional
expression. When the program reaches this line the conditional
expression is checked. If it is false (0), MATLAB skips to the end
statement and continues with the program. If the conditional
expression is true (1), MATLAB executes the group of commands
that follow between the while and end command. Then, MATLAB
jumps back to the while command and checks the conditional
expression. This looping process continues until the conditional
expression is false.
for-end / while-end loops
sum=0; i=1; sum=0;
for i=1:10 while i<=10
sum=sum+i; sum=sum+i;
end i=i+1;
fprintf('%d\n',sum); end
fprintf('%d\n',sum);
55
55
for i=1:2:10 i=1;
i=i^2; while i<=50
end i=i^2;
fprintf('%d\n',i); i=i+2;
end
81 fprintf('%d\n',i);

123
Write your name on the screen n times
(with for and while loops)
name=input('Enter name and surname ','s');
n=input('Number of times displayed on screen ');
for i=1:n
fprintf('%s\n',name);
end

name=input('Enter name and surname ','s');


n=input('Number of times displayed on screen ');
i=1;
while i<=n
fprintf('%s\n',name);
i=i+1;
end

Try deleting the line i=i+1?


Sum of consecutive numbers in the upper and
lower limit range

lower=input('Enter lower limit ');


upper=input('Enter upper limit ');
sum=0;
for i=lower:upper
sum=sum+i;
end
fprintf('The sum of consecutive numbers between %d
and %d ',lower,upper);
fprintf('is %d\n',sum);
Sum of first n terms of a series:
Use a for-end loop in a script file to calculate the sum of
the first n terms of the series:
n
(1) k k

k 1 2 k

Execute the script file for n = 4 and n = 20.


n=input('Enter the number of terms ' );
sum=0;
for k=1:n
sum=sum+(-1)^k*k/2^k;
end
fprintf('The sum of the series is: %.2f\n',sum)
Write a MATLAB program that calculates the factorial
of a number given by the user
n=input('Enter a number ');
fact=1;
for i=n:-1:1
fact=fact*i;
end
fprintf('Factorial of %d is %d\n',n,fact);
Write a MATLAB program that finds the average
and count of odd numbers in a given range
lower=input('Enter a lower limit');
upper=input('Enter a upper limit');
i=lower;
count=0;
sum=0;
while i<=upper
if mod(i,2)==1
count=count+1;
sum=sum+i;
end
i=i+1;
end
average=sum/count;
fprintf('Count is %d\n',count);
fprintf('Average is %.2f\n',average);
Write a MATLAB program that calculates the sum of
the squares of even numbers in a given range
lower=input('Enter a lower limit');
upper=input('Enter a upper limit');
sumofsquare=0;
if mod (lower,2)==1
lower=lower+1;
end
for i=lower:2:upper
sumofsquare=sumofsquare+i^2;
end
fprintf('Sum of squares of even numbers:');
fprintf('%g\n',sumofsquare);
MATLAB program to check whether a number is prime
or not
number=input('Enter a number ');
control=0;%number of positive divisors
if number<2
fprintf('%d is not prime number\n',number);
elseif number==2
fprintf('%d is prime number\n',number);
else
for i=2:number-1
if mod(number,i)==0
control=control+1;
break
end
end
if control==0
fprintf('%d is prime number\n',number);
else
fprintf('%d is not prime number\n',number);
end
end

You might also like