1 / 17

Computer Programming and Problem Solving Loops and more

What are Loops?. Simply put, loops allow the same segment of code to be run many times Whether to execute the segment again or not is determined by some truth conditionThis condition may be hidden (we will see this in for" loops). First, an interlude Debugging. MATLAB allows for breakpointsP

nowles
Download Presentation

Computer Programming and Problem Solving Loops and more

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. CS 101 – 010 Lecturer: Daniel J. Boston GITC 2315-C wednesday friday 1:00-2:30pm 2:30-4pm Computer Programming and Problem Solving Loops (and more)

    2. What are Loops? Simply put, loops allow the same segment of code to be run many times Whether to execute the segment again or not is determined by some truth condition This condition may be hidden (we will see this in “for” loops)

    3. First, an interlude – Debugging MATLAB allows for breakpoints Provides a way to “pause” execution and check the current state of your program’s variables Define each breakpoint by right-clicking a line of code and choosing “Set/Clear Breakpoint” from the menu A red circle will appear besides the breakpoint lines Start your program using “F5” or the Run button Execution will pause at each breakpoint “Step” through the program by pressing “F10” This executes one line at a time

    4. Debugging MATLAB also allows for conditional breakpoints Right-click and choose “Set/Modify Conditional Breakpoint” to set one Enter a “truth statement” or expression involving the variables used and defined inside your program up to that point Can also set MATLAB to pause on errors Debug menu -- “Stop if Errors/Warning” option

    5. Debugging A final MATLAB resource is the Code Analyzer This was formerly M-Lint Analyzes your program and tries to identify potential errors, poor usage, or obsolete features Click the “Tools” – “Code Analyzer” – “Show Code Analyzer Report” button to access

    6. Introducing: 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)

    7. Introducing: for Loop For loops have the following form: for index_variable = array_expression ... ... Code Block ... end array_expression should be an array One at a time, index_variable is set to columns drawn from array_expression When no more columns exist, program execution continues after end

    8. Introducing: for Loop The execution of a for loop follows this pattern: MATLAB generates an array using array_expression Before executing Code Block, index_variable is set to (roughly) array_expression(:,1) or the first column of array_expression After Code Block has been executed, index_variable becomes the next column of array_expression Step 3 is repeated until no columns remain in array_expression

    9. Some notes on for loops Similar to if-end statements, it is a good idea to indent the Code Block of loops Do not modify index_variable inside the for loop! If Code Block uses an array, best to pre-allocate it E.g. my_array = zeros(1,200); It is almost always faster to perform vector calculations using vector operations instead of loops This is known as “vectorization” and is one of MATLAB’s greatest features

    10. Introducing: Loop Controls break; “Breaks out” of the loop, regardless of terminating condition’s status Allows you to exit a loop early based on some specific condition continue; Skips the remaining lines of Code Block and starts again at the top of Code Block In for loops, causes index_variable to immediately assume the next column value of array_expression

    11. Nested Loops Similar to if-end, you can nest loops for index_var1 = array_expr1 ... for index_var2 = array_expr2 ... Inner Outer ... Code Code ... Block Block end % “end” of inner loop ... end % “end” of outer loop

    12. Putting it together: Redux y = zeros(20,20); for i = 1:size(y,1) % traverse rows for j = 1:size(y,2) % traverse columns if j < i continue; % “skip” low order columns end y(i, j) = i + j; end end % What does this produce?

    13. Putting it together: Redux y = zeros(20,20); i = 10; j = 10; while sum( sum( y ) ) < 200 % what’s this?! y(i, j) = 1; i = i + round( rand * 2 - 1 ); j = j + round( rand * 2 - 1 ); % fancy! if ( i < 1 || j < 1 || i > size(y,1) || j > size(y,2) ) break; end end for l = 1:size(y,1) % What kind of result do we get? for m = 1:size(y,2) fprintf('%.0f', y(l, m) ); end fprintf('\n'); end fprintf('\n');

    14. Vectorization Part of the power of MATLAB is the ability to vectorize This is process of using arrays and array math instead of loops It takes some practice, but we were vectorizing before we learned loops! One powerful technique in vectorization is the logical array mask It is an array of ones and zeros used to selectively choose elements in another array

    15. Vectorization % We want to get the square root of % numbers larger than 5. A = [1 2 3; 4 5 6; 7 8 9]; B = A > 5; > 5 =

    16. Achieving our problem statement with loops for i = 1:size(A,1) for j = 1:size(A,2) if A(i, j) > 5 A(i, j) = sqrt( A(i, j) ); end end end

    17. Achieving with vectorization A(B) = sqrt( A(B) ); A B B acts as an operation mask on A

More Related