1 / 22

IF something is true do {list of commands} Else { list of commands2} end

IF. IF something is true do {list of commands} Else { list of commands2} end. IF syntax. if (conditional statement) … elseif (conditional statement) … else … end . Conditional statements have RELATIONAL Operators p. 8:

oloughlin
Télécharger la présentation

IF something is true do {list of commands} Else { list of commands2} end

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IF IF something is true do {list of commands} Else { list of commands2} end

  2. IF syntax if (conditional statement) … elseif (conditional statement) … else … end Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…

  3. Exercise 1 Write a program that simulates flipping a coin and then tells heads or tails. Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…

  4. Exercise 1 (ex1.m) number = round(rand); if number == 1 text = 'heads'; else text = 'tails'; end text

  5. Exercise 2 Write a program that creates a 10 by 4 matrix that contains 1 to 10 on the first column and zeros on the second column using a for loop. Then, assign the following values on the second column. 1-3: 1, 4-6: 2, and 7-10: 3.

  6. Exercise 2 (data.m) A = zeros(10,4); for i = 1:10 A(i,1) = i; if i < 3.5 A(i,2) = 1; elseif 3.5 < i & i < 6.5 A(i,2) = 2; elseif 6.5 < i & i < 10.5 A(i,2) = 3; end end A

  7. Exercise 2 Cont. Create another column filled with random numbers between 1 and 3.

  8. Exercise 2 (dataV2.m) for i = 1:10 A(i,3) = round(rand*2)+1; end A

  9. Exercise 2 Compare values of 2nd and 3rd columns for each row, assign 1 on the 4th column if they are the same and 2 if they are different.

  10. Exercise 2 (dataV3.m) for i = 1:10 if A(i,2) == A(i,3) A(i,4) = 1; else A(i,4) = 2; end end A

  11. WHILE A loop for an a priori unknown number of iterations. WHILE (condition is satisfied) keep doing this. End

  12. WHILE: syntax while (condition) %if condition is false, loop is not executed, “jumps to end.” … if (condition) break %exits while end … end

  13. Exercise 3 Count how many while loops the computer take to randomly find a number > .99 Conditional statements have RELATIONAL Operators p. 8: Larger than, less than, equal to, not equal to…

  14. randcount.m counter = 1; a = rand; while a < .99 a = rand; counter = counter + 1; end a counter

  15. AND/OR logic • used in if and while loops: if A is true & B is true … end

  16. AND/OR logic • be careful about infinite loops with OR A = 1; count = 0; while A < 60 | count > 50 A = A+1; count = count+1; end Can use Ctrl C to end program if it gets stuck while running

  17. Exercise 3b • Change the while loop in Exercise 3 to exit early if it’s looped more than 50 times. You can do this using either AND or break.

  18. randcount.m counter = 1; a = rand; while a < .99 & counter < 51 a = rand; counter = counter + 1; if counter > 50 break; end end a counter

  19. Exercise 4 • Create your own ‘randperm’ using a while loop.

  20. own_randperm.m clear all input = 8; A = zeros(1,input); rand_number = round(rand*input); for i = 1:input; while ismember(rand_number, A) rand_number = round(rand*input); end A(1,i) = rand_number; end

  21. Homework 1.Write a script encrypt.m that encrypts words. --> don’t forget to add headers and comments to your code. 2. Write MessWord.m This program takes a word as input and shuffles only the inner letters of the word, leaving the first and last letter undisturbed.Alejandro --> Arjelando House --> Husoe

  22. MessWord What are the steps to undertake? 1. Figure out if the word is valid (no characters or numbers) 2. Figure out if the length is valid (if length < 3, what should happen?) 3. Use your old homework to do the rest.

More Related