GENN004: Introduction to Computers 4/15/2014
Lecture 5
Advanced Looping
while loop, Nested loops
Overview
• Program repetition
• while loop
• Nested loops
• Examples
Lecture 5: Advanced Looping 1
GENN004: Introduction to Computers 4/15/2014
Repeat a program N times
Iteration counter
First counter value
Step value
Last counter value
for i=1:1:5
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X);
else
disp(Y);
end
end
Calculate sum & average of student
grades (Program and Testing)
X=input(‘enter student grades:’); i X(i) sum
sum=0; 0
1 8 8
for i=1:length(X)
2 7 15
sum=sum+X(i); 3 9 24
end 4 5 29
avg = sum/length(X); 5 6 35
disp(sum);
disp(avg);
enter student grades: [8 7 9 5 6]
Lecture 5: Advanced Looping 2
GENN004: Introduction to Computers 4/15/2014
for Loop
• The general form of the for loop is:
for loopvar = range
action
….
end
• loopvar is the loop variable,
• “range” is the range of values through which the loop
variable is to iterate, and
• the action of the loop consists of all statements up to the
end
• To use the for loop, you should know the
range.
while Loop
• While loops have the following form:
while expression
...
... Code Block
...
end
• If expression evaluates to true, the Code Block is re-
executed
• If expression evaluates to false, execution continues
after end (Code Block is not re-executed)
Lecture 5: Advanced Looping 3
GENN004: Introduction to Computers 4/15/2014
While loop example
x=input(‘enter grade:’);
while x<0 || x>100
x=input(‘Error! enter grade:’);
end
disp(‘Finally you entered a valid grade. Thank You’);
enter grade: -7
Error! enter grade: 110
Error! enter grade: 70
Finally you entered a valid grade. Thank You
What these programs do?
N=input(‘enter a number:’); f=1;
f=1; i=1; i=1;
while i<N while f<10000
i=i+1; i=i+1;
f=f*i; f=f*i;
end end
disp(f); disp(i-1);
Lecture 5: Advanced Looping 4
GENN004: Introduction to Computers 4/15/2014
Repeating a program unknown
number of times
F=‘y’; % initialize F so the loop condition is true in the first time
While (F==‘y’)
X=input(‘enter num1:’);
Y=input(‘enter num2:’);
if X>Y
disp(X); ‘s’ is used to allow
non digit input
else
disp(Y);
end
F=input(‘do you want to continue (y/n)?’, ’s’);
end
Nested Loops
Beginning of loop1
Beginning of loop2
Beginning of loop3
% statements to be repeated
end of loop3
end of loop2
End of loop1
for i=1:L
for j=1:M
for k=1:N
% statements will be executed L*M*N times
end
end
end
Lecture 5: Advanced Looping 5
GENN004: Introduction to Computers 4/15/2014
Nested Loop Example
for i=1:2
1
disp(i); 1
for j=1:3 2
disp(j); 3
end ----
disp(‘------’); 2
end 1
2
3
----
What is the output of this program?
X=input(‘enter a number:’);
enter a number: 2
for i=1:N
1
disp(i);
1
for j=1:N
2
disp(j); ----
end 2
disp(‘------’); 1
end 2
----
Lecture 5: Advanced Looping 6
GENN004: Introduction to Computers 4/15/2014
Adding 2D arrays
X=input(‘enter 2D array1:’);
Y=input(‘enter 2D array2:’);
[R,C]=size(X); % to get the size of the array
for i=1:R
for j=1:C
Z(i,j)=X(i,j)+Y(i,j);
end
end
disp(Z);
enter 2D array1: [1 2 3; 4 5 6]
enter 2D array2: [4 5 6; 3 2 1]
5 7 9
7 7 7
Nested Loop Example
for i=1:4 j=1 2 3 4
for j=1:4 i=1 * * * * 1
fprintf(‘*’); 2 * * * * 2
end 3 * * * * 3
4 * * * * 4
disp(i);
end
Like disp but does not start newline
Lecture 5: Advanced Looping 7
GENN004: Introduction to Computers 4/15/2014
Counting duplicate elements
X=input(‘enter an array:’);
c=0;
for i=1:length(X)
for j=i+1:length(X)
if X(i) == X(j) % compare elements with each other
c=c+1;
break; % exit inner most loop
end
end
end
if c>0 1 2 4 1 2
disp(c);
else
disp(‘no duplicates’);
end
enter an array: [1 2 4 1 2]
2
Removing duplicate elements
X=input(‘enter an array:’);
k=1; % index for the array Y
for i=1:length(X)
c=0;
for j=i+1:length(X)
if X(i) == X(j) % compare each element and the next one
c=c+1;
break; % exit inner most loop
end
end
if c==0 % if no duplicate, then store it in Y
Y(k)=X(i);
k=k+1; % increment the index to point to next location in Y
end
end
disp(Y);
enter an array: [1 2 4 1 2]
4 1 2
Lecture 5: Advanced Looping 8
GENN004: Introduction to Computers 4/15/2014
Thank You
Course Site:
http://www.egypteducation.edu.eg/course/view.php?id=409
Introduction to Computers and Engineering - MATLAB
Lecture 5: Advanced Looping 9