1 / 24

Computer Programming (ECGD2102 ) Using MATLAB

Computer Programming (ECGD2102 ) Using MATLAB. Lecture (7): Control Flow (Chapter 2) Control Flow-Cont. Instructor: Eng. Eman Al.Swaity. Objectives ( CHAPTER (2):Control Flow). At the end of this lesson, you will be able to understand: Nested Loops and Nested Conditional Statements.

Download Presentation

Computer Programming (ECGD2102 ) Using MATLAB

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. Computer Programming(ECGD2102 ) Using MATLAB Lecture (7): Control Flow (Chapter 2) Control Flow-Cont. Instructor: Eng. Eman Al.Swaity

  2. Objectives (CHAPTER (2):Control Flow) At the end of this lesson, you will be able to understand: • Nested Loops and Nested Conditional Statements. • continue, break, return Commands • Vectorization

  3. Nested Loops and Nested Conditional Statements • Loops and conditional statements can be nested within themselves and each other. • This means that a loop and/or a conditional statement can start (and end) within another loop and/or conditional statement. • Every time a nested loop is typed MATLAB automatically indents the new loop relative to the outside loop.

  4. Nested Loops-Example 1

  5. Nested Loops-Example 1-cont Command Window

  6. Nested Loops-Example 2 fprintf(‘ Demonstrating nested FOR loops.\n\n‘); count=0; for i = 1:5 fprintf(‘ i = %g - j = ‘ , i); for j = 1:10 fprintf(‘ %g ‘, j); count=count+1; end fprintf( ‘ \n ‘ ); end

  7. Nested Loops-Example 2-cont Command Window The value of count is 50.

  8. Nested Loops-Example 3 Multiplication table using nested FOR loops

  9. Nested Loops-Example 3-cont Command Window

  10. Sum of factorials using a nested FOR loop Nested Loops-Example 4

  11. Sum of factorials using a nested FOR loop Nested Loops-Example 4

  12. Nested Loops-Example 4-cont Command Window

  13. continue, break, return • continue – forces current iteration of loop to stop and execution to resume at the start of next iteration. • break – forces loop to exit, and execution to resume at first line after loop. • return – forces current function to terminate, and control to be passed back to the calling function or keyboard.

  14. break Command

  15. Continue Command

  16. Break-Example 1 clc clear all for i=1:10 if i==5 break else fprintf('\nNo(%g)',i) end end Output >> No(1) No(2) No(3) No(4)

  17. Return-Example 1 clc clear all for i=1:10 if i==5 return else fprintf('\nNo(%g)',i) end end Output >> No(1) No(2) No(3) No(4)

  18. Continue -Example 1 clc clear all for i=1:10 if i==5 Continue else fprintf('\nNo(%g)',i) end end Output >> No(1) No(2) No(3) No(4) No(6) No(7) No(8) No(9) No(10)

  19. Comparison of break and return break is used to escape the current while or for loop. return is used to escape the current function.

  20. Vectorization • Vectorizationis the use of vector operations (Matlab expressions) to process all elements of a vector or matrix. Properly vectorized expressions are equivalent to looping over the elements of the vectors or matrices being operated upon. • A vectorized expression is more compact and results in code that executes faster than a non-vectorized expression. • To write vectorized code: • • Use vector operations instead of loops, where applicable • • Pre-allocate memory for vectors and matrices • • Use vectorized indexing and logical functions

  21. Vectorization Non-vectorized code is sometimes called “scalar code” because the operations are performed on scalar elements of a vector or matrix instead of the vector as a whole. Free Advice: Code that is slow and correct is always better than code that is fast and incorrect. Start with scalar code, then vectorize as needed.

  22. Replace Loops with Vector Operations

  23. Pre-allocate Memory

  24. End of the Lecture Let Learning Continue

More Related