Loop Control Statements Matlab
Loop Control Statements Matlab
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
end
>> x
x=
12345
for i=2:2:10
disp(i)
end
2
4
6
8
10
2
MATLAB 2023- 2024 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example:
for m = 10:-1:1
disp(m);
end
Example
for i=1:3
for j=1:3
y(i,j)=i+j;
end
end
>> y
234
345
456
3
MATLAB 2023- 2024 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
4
MATLAB 2023- 2024 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
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 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
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
9
MATLAB 2023- 2024 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
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 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
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