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

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Repetition Control Structure 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 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 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 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 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 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...
View Full Document

This note was uploaded on 04/27/2011 for the course ENCMP 100 taught by Professor Cockburn during the Spring '11 term at University of Alberta.

Page1 / 39

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

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online