1 Flow of control Sequential Executing instructions one by one, in exact order given Selection...
-
Upload
rosemary-hunter -
Category
Documents
-
view
217 -
download
0
Transcript of 1 Flow of control Sequential Executing instructions one by one, in exact order given Selection...
1
Flow of controlSequential
Executing instructions one by one, in exact order given
SelectionChoosing to execute a particular set of
statements depending on a condition
RepetitionExecuting a set of statements more than
one time (loops)
1
6
Nesting
Similar items contained within
e.g., matryoshka dollshttp://hancocktownlibrary.blogspot.com/2007_11_01_archive.html
7
Simple Nested IFs
if <condition A>if <condition B>
<action 1>else
<action 2>end
else<action 3>
end<action 4>
if <condition C><action 11>
elseif <condition D>
<action 12>else
<action 13>end
end<action 14>
8
Alternative to Simple Nested IF
if <condition A>if <condition B>
<action 1>else
<action 2>end
else<action 3>
end<action 4>
if < A & B ><action 1>
endif < A & ~B >
<action 2>endif < ~A >
<action 3>end<action 4>
9
Simple Nested Loops
for <control A><action 1>for <control B> <action 2>end<action 3>
end<action 4>
while <condition C><action 11>while <condition
D> <action 12> <change D>end<change C>
end<action 13>
10
A Nested Loop Example
Problem: Use a Loop to create a Matrix M with n rows and m columns
The first row elements should get values equal to the number of their respective column
The values for the elements in the first column should be the number of their respective row
The rest of the elements get values equal to the sum of the element just above them and the element to their left
The program is interactive and prompts the user to enter the values for n and m
Sample M for n=4, m=5
1 2 3 4 5
2 4 7 11 16
3 7 14 25 41
4 11 25 50 91
11
12
Recall … Design Steps for Loops
Identify a task that can be accomplished if some steps
are repeated
(These steps form the loop body)
Define a starting point
Define a stopping point
Keep track of (and measure) progress
Make sure the loop will stop!
If necessary use the “Break” command or the “Continue”
command
13
n = input ('Enter the number of rows: ');m = input ('Enter the number of columns: ');
for k = 1:n % Start of the 1st For-end Loop for h = 1:m % Start of the 2nd For-end Loop (nested loop) if k == 1 % Start of the nested "if-elseif-else-end" M(k,h) = h; elseif h == 1 M (k, h) = k; else M(k,h) = M(k,h-1) + M(k-1,h); end % End of the if statement end % End of the 2nd For-end Loop (nested loop) end % End of the 1st For-end Loop M % Display M
Solution using 2 Nested “For-Loops”
14
n = input ('Enter the number of rows ');m = input ('Enter the number of columns ');k = 1; while k <= n % Start of the 1st while-end Loop h = 1; while h <= m % Start of the 2nd while-end Loop if k == 1 % Start of the nested "if-elseif-else-end" M(k,h) = h; elseif h == 1 M (k, h) = k; else M(k,h) = M(k,h-1) + M(k-1,h); end % End of the if statement h = h + 1; end % End of the 2nd For-end Loop k = k + 1; end % End of the 1st For-end LoopM % Display M
Solution using 2 Nested “While-Loops”
Setup for Example 2
Determine whether a given number is prime:
Use the MATLAB rem() functionrem(x,y) returns the remainder from x/y
examples: rem(12, 5) is 2rem(24, 6) is 0rem(3, 5) is 3
15
Algorithm to Determine if Prime
Check that number is > 0If number is 1, it is prime!Initialize divisor = 2Repeat:
let x = rem(number,divisor)if x == 0, then x is ‘not prime’, so
stop loopotherwise, set divisor = divisor + 1
If divisor == number, then number is prime!16
Solution to Determine if Primenumber = input('Please enter a positive integer: ');
if (number <= 0) %test validity of input
disp(‘not a positive integer; program ends');
elseif (number == 1) %and we’re done
disp('1 is indeed prime');
else
divisor = 2; %initialize factor value
while ((rem(number,divisor) ~= 0))
divisor = divisor + 1;
end
if (number == divisor)
fprintf('The number %d is prime\n',number);
else
fprintf('The number %d is NOT prime\n', number);
end
end17
Example 2: Find Primes
Find all the primes in a given range of numbers
Algorithm:obtain and check the range valuesstarting with the lower end, repeat:
if number is prime, print itif range is not complete, continue
with the next number
18
What type of loop?
Solution for Find Primes in a Rangelowest = input('Please enter start of range (>=1): ');
highest = input('Please enter end of range: ');
if ((highest-lowest <= 0)||(lowest <= 1)) %test validity
disp('not an appropriate range; program ends');
else
disp('The primes within this range are: ');
for number = lowest:highest
divisor = 2; %initialize factor value
while ((rem(number,divisor) ~= 0))
divisor = divisor + 1;
end
if (number == divisor)
fprintf('%d ',number);
end
end
end19
One minor issue remains – can you find it? Hint: consider output from all cases…
Tracing Practice
20