06_repetition (1)

# 06_repetition (1) - Repetition Control Structure ENCMP 100...

This preview shows pages 1–14. Sign up to view the full content.

Repetition Control Structure

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENCMP 100 2 ©2011 by ECE, UofA Overview Repetition structures: construct for construct while Statements: command break command continue Repetition
ENCMP 100 3 ©2011 by ECE, UofA Repetition: for vs. while Loops are used when you need to repeat a set of instructions multiple times for : used when you know how many times you need to repeat the loop while : used when you need to keep repeating the instructions until a criterion is met Repetition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENCMP 100 4 ©2011 by ECE, UofA Loop for for index = expr commands to be executed (body) end the first line identifies the loop and defines an index – a number that changes on each pass through the loop; then a group of commands to be executed; finally, the end of loop is identified Repetition
ENCMP 100 5 ©2011 by ECE, UofA Example 6.1: simple for loop a simple program to print numbers for i=1:2:5 disp(['the value of i is: ', num2str(i)]); end it returns the value of i is: 1 the value of i is: 3 the value of i is: 5 Repetition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENCMP 100 6 ©2011 by ECE, UofA Loop for : more about expr expr is the loop control expression, whose result is an array columns in the array produced by expr are stored one at a time in the variable index , and then the body is executed – the loop is executed once for each column in the array produced by expr Repetition
ENCMP 100 7 ©2011 by ECE, UofA Example 6.2 for k = 1:3 a = k^2 end a = 1 a = 4 a = 9 Repetition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENCMP 100 8 ©2011 by ECE, UofA Example 6.3 for k = 1:2:6 a = 2^k end a = 1 a = 8 a = 32 Repetition
ENCMP 100 9 ©2011 by ECE, UofA Example 6.4 for k = [1 3 7] k end k = 1 k = 3 k = 7 Repetition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENCMP 100 10 ©2011 by ECE, UofA Example 6.5 for k = [1 3 7; 4 5 9] k end k = [1; 4] k = [3; 5] k = [7; 9] Repetition
ENCMP 100 11 ©2011 by ECE, UofA Example 6.6: for with matrix as expr a simple program to sum some numbers first_sum = 0; second_sum = 0; for k = [1 2 3; 10 20 33] first_sum = first_sum + k(1); second_sum = second_sum + k(2); end disp(['first sum is: ', num2str(first_sum)]); disp(['second sum is: ', num2str(second_sum)]); it returns first sum is: 6 second sum is: 63 Repetition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENCMP 100 12 ©2011 by ECE, UofA Loop for : execution 1. MATLAB generates an array by evaluating the control expression 2. the first time – the program assigns the 1 st column of the array to index , and executes the statements within the body 3. after execution of all statements, the next column is assigned to index , and execution is repeated 4. the step 3 is repeated, as long as there are additional columns in the array Repetition
ENCMP 100 13 ©2011 by ECE, UofA Example 6.7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern