You are on page 1of 11

MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬

‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

LOOPS
Many programs require iteration, or repetitive execution of a block of
statements. MATLAB has two loop statements: for and while
statements. All of the loop structures in matlab are started with a
keyword such as "for" or "while" and all of them end with the word
"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

• The ``for...end'' loop


The for loop executes a statement or group of statements a
predetermined number of times. The syntax is:
for variable = start: increment: end
statements
end
For example, this loop executes five times.
for n = 2:6
x(n) = 2 * x(n − 1);
end
Example:
for i=1:5
x(i)=i;
1
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

end
>> x
x=
12345

The default increment is 1. You can specify any increment, including


a negative one. For positive indices, execution terminates when the
value of the index exceeds the end value; for negative increments, it
terminates when the index is less than the end value.
The amount of increase and decrease can be controlled by adding a
number between the first and last value in the for clause, as shown in
the following example.

for i=2:2:10
disp(i)
end
2
4
6
8
10

2
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

You can also iterate in reverse order.

Example:
for m = 10:-1:1

disp(m);

end

Nested Loop is also supported in MATLAB and here is the following


example to show how to write Nested Loops.You can nest multiple
for loops as fallow.

Example

for i=1:3

for j=1:3

y(i,j)=i+j;

end

end

>> y

234

345

456
3
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

In the previous example, each time the first repetition sentence is


executed one time, followed by the execution of the second repetition
sentence with the number of repetitions.

Example:Find summations of even numbers between 0 and 100


sum = 0;
for i = 0 : 2 : 100
sum = sum + i;
end
sum
sum =
2550

• The ``while...end'' loop


The while loop executes a statement or group of statements repeatedly
as long as the controlling expression is true. The general form of a while
loop is:
while expression
statements
end

4
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

The controlling expression produces a logical value. If the expression


is true, the code block will be executed, and then control will return to
the while statement. If the expression is still true, the statements will
be executed again. This process will be repeated until the expression
becomes false. When control returns to the while statement and the
expression is false, the program will execute the first statement
after the end.in short words the statements are executed as long as
expression is true.

Example:
x=1;
while x<10
x
x=x+1;
end

Example:
a = 10;
% while loop execution
while (a < 20)
fprintf('value of a: %d\n', a);
a = a + 1;
end
5
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Example
%Use a while loop to calculate factorial
n=input('Enter integer positive number’');
f = 1;
while n > 1
f = f*n;
n = n-1;
end
disp(['n! = ' num2str(f)])

Example:

n = 10;
while n > 0
disp('Hello World')
n = n - 1;
end
Example:
n = 1; % press Ctrl+C to stop
while n > 0
disp('Hello World')
n = n + 1;
end
6
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Example:
x = 1;
while x < 25
disp (x);
x = x + 1;
end;
Example:Write a program to display the multiplication table
Sol:
clc;
clear;
x=1;
while x<=10
y=1;
while y<=10
z(x,y)=x*y;
y=y+1;
end
x=x+1;
end
disp(z);

7
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

The Break and Continue Commands


• Break Statement
The break statement terminates execution of for or while loop.
Statements in the loop that appear after the break statement are not
executed.In nested loops, break exits only from the loop in which it
occurs. Control passes to the statement following the end of that loop.
Syntax:
• For loop • While loop
for <iteration condition> while <condition>
statement 1 statement 1
break conditional break conditional
break break
end end

Example: Example:
s = 0; s = 0;
for i = 1: 100 x = 1;
s = s + i; while x < 100
if s > 250 s = s + x;
break; if s > 250
end; break;
end; end;
x = x + 5;
end;

8
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

• Continue statement
The continue statement is used for passing control to the next iteration
of a for or while loop. The continue statement in MATLAB works
somewhat like the break statement. Instead of forcing termination,
however, 'continue' forces the next iteration of the loop to take place,
skipping any code in between
clc value of a: 10
clear value of a: 11
a = 9; value of a: 12
%while loop execution value of a: 13
while a < 20 value of a: 14
a = a + 1; value of a: 16
if a == 15 value of a: 17
% skip the iteration value of a: 18
continue; value of a: 19
end value of a: 20
fprintf('value of a: %d\n', a);
end

% A script file to print out the odd 21


% squares backwards from 21. squared is

9
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

for i=21:-2:1 441


disp(i) .
disp('squared is') .
disp(i^2) 1
end squared is
1

Example: Write MATLAB code to print the following

clc
clear
rows = input('’How many rows do you want?’');
for R = 1:rows
for s = 1:R
fprintf('*');
end
fprintf('\n');
end

10
MATLAB 2023- 2024 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Example: Create a script file to generate (N X N) matrix in form like:

clear
N=input('Enter the square matrix size : ');
for i=1:N
for j=1:N
if mod(i+j,2)==0
a(i,j)=1;
else
a(i,j)=0;
end
end
end
disp(a)

11

You might also like