Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.

Similar presentations


Presentation on theme: "1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending."— Presentation transcript:

1 1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending on a condition Repetition Executing a set of statements more than one time (loops) 1

2 2 The “BREAK” Command When inside a loop (for or while), break terminates the execution of the loop (the whole loop, not just the last pass). When the break command appears in a loop, Matlab jumps to the end command of the loop and continues with the next command (does not go back to the for or while command of that loop). If the break command is inside a nested loop, only the nested loop containing that break is terminated. The break command is usually found within a conditional statement. In loops it provides a method to terminate the looping process if some condition is met, e.g., if the number of loops exceeds a predetermined value, or an error in some numerical procedure is smaller than a predetermined value.

3 3 Sample with Break

4 4 The “CONTINUE” Command The “Continue Command” can be used inside a “for loop” or a “while loop” to stop the present pass and start the next pass in the looping process. The “Continue Command” is usually part of a conditional statement. When MATLAB reaches a continue command, it skips to the end of the innermost loop containing the continue and then starts a new pass.

5 5 Sample with Continue

6 6 Nesting Similar items contained within e.g., matryoshka dolls http://hancocktownlibrary.blogspot.com/2007_11_01_archive.html

7 7 Simple Nested IFs if else end else end if else if else end

8 8 Alternative to Simple Nested IF if else end else end if end if end if end

9 9 Simple Nested Loops for for end end while while end end

10 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 

11 Sample M for n=4, m=5 12345 2471116 37142541 411255091 11

12 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 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 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 Loop M % Display M Solution using 2 Nested “While-Loops”

15 Setup for Example 2 Determine whether a given number is prime: Use the MATLAB rem() function rem(x,y) returns the remainder from x/y examples: rem(12, 5) is 2 rem(24, 6) is 0 rem(3, 5) is 3 15

16 Algorithm to Determine if Prime Check that number is > 0 If number is 1, it is prime! Initialize divisor = 2 Repeat: let x = rem(number,divisor) if x == 0, then x is ‘not prime’, so stop loop otherwise, set divisor = divisor + 1 If divisor == number, then number is prime! 16

17 Solution to Determine if Prime number = 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 17

18 Example 2: Find Primes Find all the primes in a given range of numbers Algorithm: obtain and check the range values starting with the lower end, repeat: if number is prime, print it if range is not complete, continue with the next number 18 What type of loop?

19 Solution for Find Primes in a Range lowest = 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 19 One minor issue remains – can you find it? Hint: consider output from all cases…

20 Tracing Practice 20


Download ppt "1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending."

Similar presentations


Ads by Google