Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the...

17
Lecture 5 Advanced Looping while loop, Nested loops

Transcript of Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the...

Page 1: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Lecture 5Advanced Looping

while loop, Nested loops

Page 2: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Overview

• Program repetition

• while loop

• Nested loops

• Examples

Page 3: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

X=input(‘enter num1:’);Y=input(‘enter num2:’);if X>Y

disp(X);else

disp(Y);end

Repeat a program N times

for i=1:1:5

end

Iteration counterFirst counter value

Step value

Last counter value

Page 4: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Calculate sum & average of student grades (Program and Testing)

X=input(‘enter student grades:’);sum=0;for i=1:length(X)

sum=sum+X(i);endavg = sum/length(X);disp(sum);disp(avg);

i X(i) sum

0

1 8 8

2 7 15

3 9 24

4 5 29

5 6 35

enter student grades: [8 7 9 5 6]

Page 5: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

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.

Page 6: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

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)

Page 7: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

While loop example

x=input(‘enter grade:’);while x<0 || x>100

x=input(‘Error! enter grade:’);enddisp(‘Finally you entered a valid grade. Thank You’);

enter grade: -7Error! enter grade: 110Error! enter grade: 70Finally you entered a valid grade. Thank You

Page 8: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

What these programs do?

N=input(‘enter a number:’);

f=1; i=1;

while i<N

i=i+1;

f=f*i;

end

disp(f);

f=1;

i=1;

while f<10000

i=i+1;

f=f*i;

end

disp(i-1);

Page 9: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

X=input(‘enter num1:’);Y=input(‘enter num2:’);if X>Y

disp(X);else

disp(Y);end

Repeating a program unknown number of times

F=input(‘do you want to continue (y/n)?’, ’s’);

While (F==‘y’)

end

F=‘y’; % initialize F so the loop condition is true in the first time

‘s’ is used to allow non digit input

Page 10: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Nested Loops

Beginning of loop1Beginning of loop2

Beginning of loop3% statements to be repeated

end of loop3end of loop2

End of loop1

for i=1:Lfor j=1:M

for k=1:N% statements will be executed L*M*N times

endend

end

Page 11: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Nested Loop Example

for i=1:2disp(i);for j=1:3

disp(j);enddisp(‘------’);

end

1123----2123----

Page 12: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

What is the output of this program?

X=input(‘enter a number:’);for i=1:N

disp(i);for j=1:N

disp(j);enddisp(‘------’);

end

enter a number: 2112----212----

Page 13: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Adding 2D arrays

X=input(‘enter 2D array1:’);Y=input(‘enter 2D array2:’);[R,C]=size(X); % to get the size of the arrayfor i=1:R

for j=1:CZ(i,j)=X(i,j)+Y(i,j);

endenddisp(Z);

enter 2D array1: [1 2 3; 4 5 6]enter 2D array2: [4 5 6; 3 2 1]5 7 97 7 7

Page 14: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Nested Loop Example

for i=1:4

for j=1:4

fprintf(‘*’);

end

disp(i);

end

* * * * 1

* * * * 2

* * * * 3

* * * * 4

Like disp but does not start newline

j=1 2 3 4

i=1234

Page 15: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

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

endend

endif c>0

disp(c);else

disp(‘no duplicates’);end

enter an array: [1 2 4 1 2]2

1 2 4 1 2

Page 16: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Removing duplicate elementsX=input(‘enter an array:’);k=1; % index for the array Yfor i=1:length(X)

c=0;for j=i+1:length(X)

if X(i) == X(j) % compare each element and the next onec=c+1;break; % exit inner most loop

endendif 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

endenddisp(Y);

enter an array: [1 2 4 1 2]4 1 2

Page 17: Lecture 5 Advanced Looping - Cairo University · 2020-05-23 · forLoop •The general form of the for loop is: for loopvar = range action …. end •loopvar is the loop variable,

Thank You

Course Site: http://scholar.cu.edu.eg/?q=eldeib/classes/genn004-computers-engineers

Computers for Engineers – GENN004