1 / 26

for – while – switch

for – while – switch. ผศ.ดร.อนันต์ ผลเพิ่ม Anan Phonphoem http://www.cpe.ku.ac.th/~anan anan@cpe.ku.ac.th. Loops. Repeating a calculation for a number of times Each repetition of the loop is a pass Two types of loops (In MATLAB) for loop while loop. Set loop variable = m.

juro
Download Presentation

for – while – switch

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

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

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

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

  5. break command To abnormally jump out of the loop before its end

  6. Find the answer of the program cmd1.m Results for num = 10:-2:0 disp(num); temp = 2*num - 10; solution = temp + 5; end solution = solution - 10 >> cmd1 10 8 6 4 2 0 solution = -15

  7. break example cmd2.m Results % showing ‘break’ command for num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5; end solution = solution - 10 >> cmd2 10 8 6 4 solution = -3

  8. continue command To skip for some cases and continue execute the rest of the loop For example, skip the error that might happen

  9. continue example cmd3.m Results a = [100 0 10 -10]; for k = 1:length(a) solution = 1000/a(k) end >> cmd3 solution = 10 Warning: Divide by zero. > In cmd3 at 3 solution = Inf solution = 100 solution = -100

  10. continue example cmd4.m Results % showing ‘continue’ command a = [100 0 10 -10]; for k = 1:length(a) if (a(k)== 0) continue end solution = 1000/a(k) end >> cmd4 solution = 10 solution = 100 solution = -100

  11. while loop Looping process is terminated based on the specified condition

  12. Set loop variable = m False condition True Statements Without the , no way to get out of the loop. It’s called “infinite loop” Increment variable while loops while condition statements end

  13. while example (I) cmd5.m while k > 0 disp(k); end Results >> cmd5 ??? Undefined function or variable "k". Error in ==> cmd5 at 1 while k > 0

  14. while example (II) cmd6.m k = 5; while k > 0 disp(k); end Results >> cmd6 5 5 5 5 … To break the program Press “Ctrl-C”

  15. while example (III) cmd7.m Results k = 5; while k > 0 disp(k); k = k-1; end >> cmd7 5 4 3 2 1

  16. while and for loop cmd2.m Rewrite the program By using “while” % showing ‘break’ command for num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5; end solution = solution - 10

  17. while and for loop cmd8.m % using while num = 10; temp = 2*num - 10; while temp > 0 disp(num); solution = temp+5; temp = 2*num -10; num = num-2; end solution = solution - 10

  18. condition1 False True condition2 True False Statement1 Statement2 Statement3 Nested Logic if logical exp1 statement g1 elseif logical exp2 statement g2 else statement g3 end

  19. if-elseandswitchcomparison if logical exp1 statement g1 elseif logical exp2 statement g2 else statement g3 end switch input expression case value1 statement g1 case value2 statement g2 otherwise statement g3 end

  20. a == 20 False True c == 30 c = a * 2 True False c = a + 2 c = 0 Nested Logic – (Example) if a == 20 c = a * 2 elseif a == 30 c = a + 2 else c = 0 end switch a case 20 c = a * 2 case 30 c = a + 2 otherwise c = 0 end

  21. switch switch input expression case value1 statement group 1 case value2 statement group 2 case value1 statement group 3 ... otherwise statement group n end

  22. switch example (I) cmd9.m response = input(‘Type like, hate, or ok: ’,’s’); switchresponse case‘like’ disp(‘I really like it’); case‘hate’ disp(‘I do not like it’); case ‘ok’ disp(‘It is ok’); otherwise disp(‘Your enter is wrong’); end

  23. switch example (I) >> cmd9 Type like, hate, or ok: like I really like it >> cmd9 Type like, hate, or ok: hate I do not like it >> cmd9 Type like, hate, or ok: abc Your enter is wrong >> cmd9 Type like, hate, or ok: Like Your enter is wrong

  24. Can you make it better ?

  25. switch example (II) cmd10.m while true response = input(‘Type like,hate,ok, or quit:’,’s’); response = lower(response); switchresponse case ‘like’ disp(‘I really like it’); case ‘hate’ disp(‘I do not like it’); case ‘ok’ disp(‘It is ok’); case ‘quit’ break otherwise disp(‘Your enter is wrong’); end %end switch end %end while

  26. switch example (II) >> cmd10 Type like, hate, ok, or quit: like I really like it Type like, hate, ok, or quit: Hate I do not like it Type like, hate, ok, or quit: abc Your enter is wrong Type like, hate, ok, or quit: quit >>

More Related