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

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

View Full Document Right Arrow Icon
Repetition Control Structure
Image of page 1

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

View Full Document Right Arrow Icon
ENCMP 100 2 ©2011 by ECE, UofA Overview Repetition structures: construct for construct while Statements: command break command continue Repetition
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
ENCMP 100 9 ©2011 by ECE, UofA Example 6.4 for k = [1 3 7] k end k = 1 k = 3 k = 7 Repetition
Image of page 9

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

View Full Document Right Arrow Icon
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
Image of page 10
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
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
ENCMP 100 13 ©2011 by ECE, UofA Example 6.7
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern