Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.

Similar presentations


Presentation on theme: "1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop."— Presentation transcript:

1 1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop 1

2 1. Definition and Structure Generally used for a fixed number of iterations Main structure: for variable = array block of code end One rule: “The for loop will run the number of elements in the array” Great advantage: start-increment-stop are ALL WRITTEN on the 1 st line. 2

3 2. Small Example 1 Display all the numbers from 3 to 120, by ones for x = 3:120 disp(x); end 3 >> 3 4 5 6 7 …

4 Example 1, the range operator Display all the numbers from 3 to 120, by ones for x = 3:120 disp(x); end 4 The colon operator “:” is new to you. It is called the range operator. The range is from 3 to 120 “in steps of one”. >> 3 4 5 6 … 120 >>

5 Example 1, in steps of… Display all the numbers from 3 to 120, in steps of 5 for x = 3:5:120 disp(x); end 5 The middle number indicates which “increment” to use when counting. In this case, each time the loop runs, x is increased by 5. Note that 120 is NOT attained. The implied condition was to stop at 120, but 118+5 >120, hence does not show up! >> 3 8 13 18 … 113 118

6 3. Simplified Structure for loops use the range operator to specify a range of values to loop over (to iterate) Structure: for variable = start : increment : end block of code end The increment is optional – defaults to +1 if omitted 6

7 4. Short Additional Examples In all examples below, there is an implied loop body (code-block), and end % loop 10 times for x = 1:10 % Values CAN be decimals for y = 1.2 : 2.3 : 100.5 % Can use negative steps for z = 10:-2:0 7

8 Additional Examples, cont. The loop continues as long as the value is “in the range” specified for ctr=1:3:10% as long as 1<=ctr<=10 (1, 1+3=4, 4+3=7, 7+3=10, 10+3=13) for val=-5:2:5% as long as -5<=val<=5 (-5, -5+2=-3, -3+2=-1, -1+2=1, 1+2=3, 3+2=5, 5+2=7) for thing=5:-3:0 % as long as 5>=thing>=0 (5, 5-3=2, 2-3=-1) 8

9 Ex2, Average, algorithm Prompt the user for the number of temperature samples s/he collected during take-off. Prompt for each temperature value, then compute and display the average with a precision of 5 decimal places. Assume no errors will be made when user enters each value. % prompt number of samples % start sum at zero % loop that number of times exactly % ask for a temperature % keep a “running total” % calculate/display average 9

10 Ex2, Average, display result % prompt number of samples samples = input(‘How many samples were taken (WHOLE NUMBER)? ’); sum = 0; %start sum at zero % loop that number of times exactly for ct = 1:samples % ask for a temperature temperature = input(‘Temperature: ’); % keep a “running total” sum = sum + temperature; end % calculate/display average fprintf(‘Given these %d temperatures, the average is %.5f\n’, samples, sum/samples); 10

11 6. Common errors While it is possible to change the loop counter in a for loop, it is not a good idea since the value will be changed back in the next loop iteration Example: display the counter %loop 5 times REGARDLESS OF WHAT HAPPENS IN THE LOOP for ct =1:5 fprintf(‘%d\n’, ct); %display counter ct = ct * 3; %multiply counter by 3… end 11

12 Common error, cont. Expected Output: Loop 5 times Multiply the counter by 3 each time 12 >> 1 3 9 27 81 >>

13 Common error, cont. Expected Output:Actual Output: The loop variable ( ct ) changed back to what was written at the beginning of the loop: for ct = 1:5 Which means: “for ct equals 1 to 5 in steps of 1” 13 >> 1 2 3 4 5 >> 1 3 9 27 81 >>

14 Common error, cont. Had the intention been to display the multiples of 3 each time, a simple swap in the code-block would do! In order, the variable ct will take the following values. Underlined are the ones actually displayed on the screen: 1, 3, 2, 6, 3, 9, 4, 12, 5, 15 14 %loop 5 times REGARDLESS OF WHAT HAPPENS IN THE LOOP for ct =1:5 ct = ct * 3; %multiply counter by 3… fprintf(‘%d\n’, ct); %display counter ct = ct * 3; %multiply counter by 3… end

15 Wrapping Up The for loop is used when the number of iterations (repetition) is known in advance. “Known in advance” does not necessarily mean a fixed number. The user can input a value, then this value (stored in a variable) becomes the known number of iteration. The simplified structure is: for variable = start: increment: stop Block of code end Omitting the increment defaults to an incrementation of +1. There is no method to change the number of iteration during the execution of a for loop. Practice, practice, practice!! 15


Download ppt "1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop."

Similar presentations


Ads by Google