240 likes | 364 Views
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.
E N D
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. • continue, break, return Commands • Vectorization
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.
Nested Loops-Example 1-cont Command Window
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
Nested Loops-Example 2-cont Command Window The value of count is 50.
Nested Loops-Example 3 Multiplication table using nested FOR loops
Nested Loops-Example 3-cont Command Window
Sum of factorials using a nested FOR loop Nested Loops-Example 4
Sum of factorials using a nested FOR loop Nested Loops-Example 4
Nested Loops-Example 4-cont Command Window
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.
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)
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)
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)
Comparison of break and return break is used to escape the current while or for loop. return is used to escape the current function.
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
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.
End of the Lecture Let Learning Continue