Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.

Similar presentations


Presentation on theme: "Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True."— Presentation transcript:

1 Review for Exam2 Key Ideas 1

2 Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True B.False C.Impossible to determine (22 > x) && (x > 29.3) A.True B.False C.Impossible to determine (x = 2) A.True B.False C.Impossible to determine 2 What is the result of the following statement?

3 Key Ideas: if statements Decision (Conditional) Structure – To execute some part of code under certain circumstances only – To skip some part of a code 3 if elseif … else end

4 Key Ideas: switch Statements Decision (Conditional) Structure – To compare a single variable with multiple cases (equality) – To execute some part of code under the first matching case – To skip rest of a code 4 switch variable case specification 1. case specification n otherwise end

5 Key Ideas: Loops Matlab has two loops, one for each method explained previously: while – A CONDITIONAL loop. Generic, all-purpose. Best used when the programmer does not know how many times the block of code needs to repeat. – Repeats while CONDITION is true. for – A COUNTING loop. Best used when the programmer “knows” how many times the block of code needs to repeat. 5

6 Key Ideas: while Loop A while loop repeats a block of code while a condition is true. Loop variable(s) Structure: Initialization, while condition, code to be repeated, update of condition, end Common Uses: – trap the user until a valid input is given (relational operators, even, odd, integers, etc.) – running total until a condition is met Infinite loop occurs when the loop condition is never updated to a false condition while in the loop – To stop an infinite loop: Ctrl-C 6

7 Key ideas: for Loop The for loop is a counting loop The while loop can always be used BUT…the for loop is meant to count – The startvalue:step:endValue contains all necessary values for the loop to work. Nested for loops are absolutely possible! Actually, any construct ( if, switch, while, for ) can now be nested within each other. 7

8 Review for Exam2 Questions 8

9 Review Questions Loops:  Match the following a.for1. does not exist in Matlab b.while2. number of iterations is known c.do/while3. unknown number of iterations 9

10 Review Questions  After prompting the user how many values should be entered, what loop is most appropriate to prompt for each grade separately?  Nested within the 1 st loop, which loop can be used to trap the user until s/he enters a valid value? 10 a.for b.while c.switch d.if a.for b.while c.switch d.if

11 Review Questions Loops: X = -1; while X <=0 Y = X + 1; end The code above will result in an infinite loop 11 a.True b.False

12 Review Questions Loops: X = -1; while X <=0 x = X + 1; end The code above will result in an infinite loop 12 a.True b.False

13 Review Questions Loops: X = -1; while X <=0 X = X + 1; end The code above will result in an infinite loop 13 a.True b.False

14 Review Questions X1 = 4; X2 = 7; while ____(x1-x2)>0.0002 % code.. end  In numerical methods, what function is commonly used to check if two values are “close enough”? 14

15 Review Questions Using the following code: for r = 1:3 for c = 1:4 x = input(‘An integer: ‘); end  How many times will the input() command execute?  In general, what is the result? 15

16 Review Questions Using the following code: for position = 1:2:14 disp(‘*’) end  How many stars will print to the screen? 16

17 Review Questions  Using the following code, fill in the blanks to answer. result = 0; for x = 3:5 for y = 1:2 for z = 10:10:20 result = result + x*y*z; end result = 0 + 3*1*10+ ______ + _____ + ______ + ______ + _____ + ____ + _____. 17

18 Review Questions  How many stars will print to the screen? _____ for position = 14:2:1 disp(‘*’) end 18

19 Review Questions Library functions:  What is the range of the following equations: a. floor(rand*12+3) b. ceil(rand*5+1) c. rand*10+5 19

20 Review Questions Modulus:  The following code Is true only if x is 20 a.odd b.even c.fraction

21 Example Problem Below are two formulas for calculating π: Write a MATLAB program that – 1) Calculate the value of pi using the first formula and let the term to add up until the error is within 0.000001. Keep track the number of terms. – 2) Calculate the value of pi using the second formula with the same number of terms. – 3) Output pi values from step 1 and 2 (8 decimal places), as well as the error in the step 2 as a percentage (4 decimal places). 21

22 Example Problem, cont’d Write a MATLAB program that – 1) Calculate the value of pi using the first formula and let the term to add up until the error is within 0.000001. Keep track the number of terms. – 2) Calculate the value of pi using the second formula with the same number of terms. – 3) Output pi values from step 1 and 2 (8 decimal places), as well as the error in the step 2 as a percentage (4 decimal places). 22

23 STUDY HARD! Practice tonight. Take the notes you just took, and code the example! The more practice, the easier it gets! 23


Download ppt "Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True."

Similar presentations


Ads by Google