Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Matlab L3 Program Flow

5,438 views

Published on

  • Be the first to comment

Matlab L3 Program Flow

  1. 1. Civil Engineering Skills Computer Programming CIV1900 Dr Sam Clarke Control of Program Flow
  2. 2. What is Program flow?Learning outcomes & Aims◦ What program flow is◦ Why it may be useful to control the flow◦ How to control program flow using sequential logic “Making Decisions”
  3. 3. Program Flow Process Decision Data
  4. 4. Program Flow – simple algorithm Data in A=[1,2;3,4] Data processing A=A*2 B=A Data out B=[2,4;6,8]Pure logic
  5. 5. Program Flow – another algorithm Data in A=[1,2;3,4] if A(1,1)==1 A=A*2 else Statement A=A*3 True False endProcess A Process B B=A 1st time B=[2,4;6,8] Data out 2nd time B=[6,12;18,24] Decisions give flexibility
  6. 6. Making Decisions– why? • When two or more possible outcomes are required • Decisions are made based on a conditional tests • if-then • if-then-else....
  7. 7. if-thenIf this lecture is too boring then I will fallasleepif expression (this lecture is too boring)then statement (I will fall asleep) Let’s code this up
  8. 8. if-thenIf this lecture is too boring then I will fallasleep relational operatorif boredom > tiredness student = ‘asleep’end then is implied by the next line
  9. 9. if-then-elseIf I fall asleep in lectures then I will notunderstand the lab class otherwise I willbe able to complete the class quickly.if expression (I fall asleep in lectures)then statement (I will not understand the lab)else statement (I will be able to complete the class quickly)
  10. 10. if-then-elseIf I fall asleep in lectures then I will notunderstand the lab class otherwise I willbe able to complete the class quickly. relational operatorif boredom > tiredness student = ‘confused in lab’else student = ‘going home early’end
  11. 11. Relational operators Operator Description < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to
  12. 12. Relational operatorsCan be used independently of if-else A = 12; B = 18; C = A > BRemember = is an assignment C = 0 False = 0, True = 1
  13. 13. Logical operators Operator Description & True if all relations are (AND) true | True if at least one (OR) relation is true ~ True if all relations are (NOT) false
  14. 14. More if-then-elseConditionals with more than 2 cases canbe built as well: if (expression1) statement1 elseif (expression2) statement2 else statement3 end
  15. 15. More if-then-else (Practical 1)m=randn(100,1); loops next weekfor loop1=1:100 if m(loop1)>2 n(loop1,1)=2; elseif m(loop1)<-2 n(loop1,1)=0; else n(loop1,1)=1; endendo=find(n==0);p=find(n==2);q=length(o)+length(p);

×