Matlab L3 Program Flow

  • 3,601 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,601
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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 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. 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-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. 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. 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. 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. 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 operatorsCan be used independently of if-else A = 12; B = 18; C = A > BRemember = 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-elseConditionals with more than 2 cases canbe 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 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);