1 / 20

Conditional Statements + Loops

Conditional Statements + Loops. ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th. Overview. Condition statement If If-else If-elseif Nested Logic Loops for while. Terminator. Process. Input/output. Decision. Connector. Flow line. Flowcharts.

len-lynn
Download Presentation

Conditional Statements + Loops

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. Conditional Statements + Loops ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th

  2. Overview • Condition statement • If • If-else • If-elseif • Nested Logic • Loops • for • while

  3. Terminator Process Input/output Decision Connector Flow line Flowcharts • Graphical representation of algorithm

  4. Flowchart example Start Read width Read length Total = width + length If total ~= 0 No Yes show total End

  5. If I have free time, I will go to visit my friend . (period) If Statement(In real life)

  6. If logical expression statements end condition False True Statement If Statement (In MATLAB)

  7. a <= 30 False True total = 2*a If a <= 30 total = 2*a end If Statement(Example)

  8. If-else Statement (In real life) If I have free time, I will go to visit my friend else, I will send an email to her . (period)

  9. condition True False Statement1 Statement2 If-else Statement (In MATLAB) If logical expression statement group 1 else statement group 2 end

  10. a <= 30 True False total = 2*a total = a+10 If-else Statement (Example) If a <= 30 total = 2*a else total = a + 10 end

  11. Nested Logic - I (In real life) If I have free time, I will go to visit my friend If she is not there I will leave a message . (period)

  12. False condition1 True Statement1 False condition2 True Statement2 Nested Logic – I (In MATLAB) If logical expression 1 statement group 1 If logical expression 2 Statement group 2 end end

  13. False a <= 30 True c = a + 10 False c <= 12 True c = 0 Nested Logic – I (Example) If a <= 30 c = a + 10 If c <= 12 c = 0 end end

  14. Nested Logic – II (In real life) If I have free time, I will go to visit my friend elseif I can access computer I will send her an email else I will leave her a note . (period)

  15. condition1 False True condition2 True False Statement1 Statement2 Statement3 Nested Logic – II (In MATLAB) If logical expression 1 statement group 1 elseif logical expression 2 statement group 2 else statement group 3 end

  16. a <= 30 False True c >= 20 True False c = a * 2 d = c d = 0 Nested Logic – II (Example) If a <= 30 c = a * 2 elseif c >= 20 d = c else d = 0 end

  17. Loops • Repeating a calculation for a number of times • Each repetition of the loop is a pass • Two types of loops (In MATLAB) • forloop • while loop

  18. Set loop variable = m for loop variable = m:s:n statements end True Variable > n False Statements Increment variable by s for loops

  19. for a = 1:2:10 total = total + a end for loops (Example) a = 1 True a > 10 False total = total +a a = a + 2

  20. for loop variable = m:s:n statements end for loops Note: for a = 10:-2:0 c = a * 2 end for a = 1:100 c = a * 2 end for a = 10:2:5 c = a * 2 end for a = 2:2 c = a * 2 end for a = 2:1.5:20 c = a * 2 end

More Related