Your SlideShare is downloading.
×

- 1. Civil Engineering Skills Computer Programming CIV1900 Dr Sam Clarke Control of Program Flow
- 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. Program Flow Process Decision Data
- 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. 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 end Process 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. 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. if-then If this lecture is too boring then I will fall asleep if expression (this lecture is too boring) then statement (I will fall asleep) Let’s code this up
- 8. if-then If this lecture is too boring then I will fall asleep relational operator if boredom > tiredness student = ‘asleep’ end then is implied by the next line
- 9. if-then-else If I fall asleep in lectures then I will not understand the lab class otherwise I will be 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. if-then-else If I fall asleep in lectures then I will not understand the lab class otherwise I will be able to complete the class quickly. relational operator if boredom > tiredness student = ‘confused in lab’ else student = ‘going home early’ end
- 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. Relational operators Can be used independently of if-else A = 12; B = 18; C = A > B Remember = is an assignment C = 0 False = 0, True = 1
- 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. More if-then-else Conditionals with more than 2 cases can be built as well: if (expression1) statement1 elseif (expression2) statement2 else statement3 end
- 15. More if-then-else (Practical 1) m=randn(100,1); loops next week for loop1=1:100 if m(loop1)>2 n(loop1,1)=2; elseif m(loop1)<-2 n(loop1,1)=0; else n(loop1,1)=1; end end o=find(n==0); p=find(n==2); q=length(o)+length(p);